合并相似列表
我有这些列表:
{'HH1': ['x'], 'HH2': ['y', 'x'], 'HH3': ['x', 'z'], 'HH4': ['x'], 'HH5': ['x'], 'HH6': ['x'], 'HH7': ['x'], 'HH8': ['x', 'y', 'z'], 'HH9': ['x'], 'HH10': ['x', 'y'], 'HH11': ['x'], 'HH12': ['x'], 'HH13': ['x'], 'HH14': ['x'], 'HH15': ['x', 'y'], 'HH16': ['x', 'y'], 'HH17': ['x', 'y'], 'HH18': ['x']}
我想要遍历这些列表,做到:
- 计算相似组合的数量(i)
- 创建一个新列表,里面每个组合的名字是:n=i
输出应该是:
n=11: ('x')
n=5: ('x', 'y')
n=1: ('x', 'z')
n=1: ('x', 'y', 'z')
我好像一直做不对。
**1. **我试过这个,但它跳过了一个组合
(n=1: ('x', 'z'))
。
from collections import Counter
from itertools import combinations
# Counting occurrences of each combination of fuel types
combination_count = Counter()
unique_combinations = set()
for fuel_list in hfuels.values():
for r in range(1, len(fuel_list) + 1):
for combination in combinations(fuel_list, r):
unique_combinations.add(tuple(sorted(combination)))
# Creating new lists for each combination
renamed_lists = {}
for combination in unique_combinations:
count = sum(1 for fuel_list in hfuels.values() if set(combination) == set(fuel_list))
if count:
renamed_lists[f"n={count}"] = list(combination)
# Printing the renamed lists
for name, fuel_list in renamed_lists.items():
print(f"{name}: {fuel_list}")
**Outcome:**
n=1: \['z', 'y', 'x'\]
n=5: \['y', 'x'\]
n=11: \['x'\]
**2. **我也试过这个,但它计算的是出现次数,而不是组合。
from collections import Counter from itertools import combinations
# Counting occurrences of each combination of fuel types
combination_count = Counter()
unique_combinations = set()
for fuel_list in hfuels.values():
for r in range(1, len(fuel_list) + 1):
for combination in combinations(fuel_list, r):
combination_count[tuple(sorted(combination))] += 1
unique_combinations.add(tuple(sorted(combination)))
# Creating new lists for each combination
renamed_lists = {}
for combination in unique_combinations:
count = combination_count[combination]
if count:
renamed_lists[f"n={count}"] = list(combination)
# Printing the renamed lists
for name, fuel_list in renamed_lists.items():
print(f"{name}: {fuel_list}")
结果:
n=1: \['z', 'y', 'x'\]
n=2: \['z'\]
n=6: \['y'\]
n=18: \['x'\]
2 个回答
0
你可以这样做:
dct = {
"HH1": ["x"],
"HH2": ["y", "x"],
"HH3": ["x", "z"],
"HH4": ["x"],
"HH5": ["x"],
"HH6": ["x"],
"HH7": ["x"],
"HH8": ["x", "y", "z"],
"HH9": ["x"],
"HH10": ["x", "y"],
"HH11": ["x"],
"HH12": ["x"],
"HH13": ["x"],
"HH14": ["x"],
"HH15": ["x", "y"],
"HH16": ["x", "y"],
"HH17": ["x", "y"],
"HH18": ["x"],
}
cnt = {}
for v in dct.values():
t = tuple(sorted(v))
cnt[t] = cnt.get(t, 0) + 1
for k, v in cnt.items():
print(f"n={v}: {k}")
输出结果是:
n=11: ('x',)
n=5: ('x', 'y')
n=1: ('x', 'z')
n=1: ('x', 'y', 'z')
1
你的问题是,字典里的键必须是唯一的(你用的是字典,不是列表),所以不能有两个(或更多)值使用相同的 'n=1' 这个键。
如果你把列表当作键来用,那么你可以用值来计算出现的次数。
像下面这样
HH_dict = {'HH1': ['x'], 'HH2': ['y', 'x'], ... }
unique = {}
for element in HH_dict:
elem_tuple = tuple(sorted(HH_dict[element]))
if elem_tuple not in unique.keys():
unique[elem_tuple] = 1
else:
unique[elem_tuple] += 1
for key, value in sorted(unique.items(), key=lambda item: item[1], reverse=True):
print(f"n={value}: {key}")
就可以打印出你想要的结果。