我有这些 list :

{'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')).

从Collection 导入计数器
从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):
            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. * * 我也试过这个方法,但它计算的是发生而不是组合.

从Collection 导入计数器 从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}")

Outcome:

n=1: \['z', 'y', 'x'\]
n=2: \['z'\]
n=6: \['y'\]
n=18: \['x'\]

推荐答案

你的问题是,键在字典中必须是唯一的(这是你使用的,而不是列表),所以你不能有两个(或更多)值与相同的'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}")

可以打印出你要找的输出.

Python相关问答推荐

时间序列分解

如何在箱形图中添加绘制线的传奇?

Excel图表-使用openpyxl更改水平轴与Y轴相交的位置(Python)

如何使用LangChain和AzureOpenAI在Python中解决AttribeHelp和BadPressMessage错误?

python中的解释会在后台调用函数吗?

用渐近模计算含符号的矩阵乘法

Tkinter菜单自发添加额外项目

判断solve_ivp中的事件

Python pint将1/华氏度转换为1/摄氏度°°

Python日志(log)模块如何在将消息发送到父日志(log)记录器之前向消息添加类实例变量

无法在Spyder上的Pandas中将本地CSV转换为数据帧

为什么Visual Studio Code说我的代码在使用Pandas concat函数后无法访问?

如何在Python中从html页面中提取html链接?

将字节序列解码为Unicode字符串

为什么我的scipy.optimize.minimize(method=";newton-cg";)函数停留在局部最大值上?

BeatuifulSoup从欧洲志愿者服务中获取数据和解析:一个从EU-Site收集机会的小铲子

查找数据帧的给定列中是否存在特定值

删除另一个div中的特定div容器

了解如何让库认识到我具有所需的依赖项

为什么在安装了64位Python的64位Windows 10上以32位运行?