python - 从字典创建字典列表
问题描述
我有一本字典
d_1 = { 'b':2, 'c':3, 'd':6}
如何通过将字典元素的组合作为字典来创建字典列表?前任:
combs = [{'b':2}, { 'c':3}, {'d':6}, {'b':2, 'c':3}, {'c':3, 'd':6}, {'b':2, 'd':6}, { 'b':2, 'c':3, 'd':6}]
解决方案
使用下面的循环,简单地从range
:中获取所有数字[1, 2, 3]
,然后简单地使用itertools.combinations
andextend
来适应它们,而不是在最后获取字典而不是元组:
ld_1 = [{k:v} for k,v in d_1.items()]
l = []
for i in range(1, len(ld_1) + 1):
l.extend(list(itertools.combinations(ld_1, i)))
print([i[0] for i in l])
推荐阅读
- angular - Angular - 无法将数组返回到 ng-select
- python - 在 python 中为分类问题生成合成数据的正确开始
- jhipster - "jhipster 中的邮件国际化"
- azure - 字符串未被 Azure DevOps 识别为有效的 DateTime 错误
- python - 使用 PdfFileReader 时不显示文本
- android - 如何获取每个 StorageVolume 的免费大小和总大小?
- node.js - 如何从 AWS Lambda (Node.JS) 调用 Oracle 存储过程?
- javascript - CountUp.js:没有开始计数
- node.js - 在带有 .desktop 文件的树莓派上启动 node.js 终端应用程序
- powershell - 如何使用 PowerShell 检查文件包含并重命名它?