合并类似列表

问题描述 投票:0回答:2

我有这些清单:

{'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']}

我想迭代它们以:

  1. 数出相似组合的数量(i)
  2. 为每个组合创建一个新列表,命名为: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。 **我也尝试过这个,但它会计算出现次数而不是组合。

从收藏进口柜台 从 itertools 导入组合

# 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'\]
python list combinations
2个回答
0
投票

您的问题是键在字典中必须是唯一的(这是您正在使用的,而不是列表),因此您不能有两个(或更多)具有相同“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}")

可以打印您正在寻找的输出。


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')
© www.soinside.com 2019 - 2024. All rights reserved.