python - 一组 dicts 的 itertools.permutations
问题描述
在我问我的问题之前,我想让你们都知道我是一个初学者编码器,我感谢所有输入,所以提前感谢你的帮助。
假设我有以下对象:
set: [
{'id': 1
'type': a
},
{'id': 2
'type': a
},
{'id': 3
'type': b
},
{'id': 4
'type': b
},
{'id': 5
'type': a
}
]
我怎么能用它itertools.permutations
来计算(并最终复制)'type'
密钥的所有组合?即有一个集合,其中 ids 3 和 5 具有类型b
,其余的a
,一个集合,其中 ids 2 和 5 具有b
和其余的a
,等等。
此外,是否可以在特定条件下计算组合(例如,id 1 和 2 必须始终是a
所有组合的类型)?
谢谢大家
解决方案
基于John 已有的内容,我们可以通过在计算排列之前从类型列表中删除受约束的类型来限制排列。然后,您可以将类型重新插入到约束定义的每个排列中。以下示例脚本将返回带有约束的排列:“ids 1 和 2 必须始终为所有组合的类型 a”。随意删除打印语句。
from itertools import permutations
# these define which types must be located at the given id (index + 1)
constraints = [
{'id': 1, 'type': "a"},
{'id': 2, 'type': "a"}
]
# this is the list of types to permute
typeset = [
{'id': 1, 'type': "a"},
{'id': 2, 'type': "a"},
{'id': 3, 'type': "b"},
{'id': 4, 'type': "b"},
{'id': 5, 'type': "a"}
]
# we create lists of types to permute and remove the ones that are constrained
types_to_permute = [d["type"] for d in typeset]
constraint_types = [d["type"] for d in constraints]
for constraint_type in constraint_types:
types_to_permute.remove(constraint_type)
print(types_to_permute)
# we calculate the permutations for the unconstrained types
permutations = list(permutations(types_to_permute, r=None))
permutations = [list(permutation) for permutation in permutations]
print(permutations)
# we insert the constrained types in their given locations
for permutation in permutations:
for constraint in constraints:
permutation.insert(constraint["id"] - 1, constraint["type"])
print(permutations)
# we reconstruct the permutations in the orignal format (list of dictionaries)
final_permutations = []
for permutation in permutations:
final_permutation = []
for index, type_object in enumerate(permutation):
final_permutation.append({"id": index + 1, "type": type_object})
final_permutations.append(final_permutation)
print(final_permutations)
最终的排列将是:
[
[
{'id': 1, 'type': 'a'},
{'id': 2, 'type': 'a'},
{'id': 3, 'type': 'b'},
{'id': 4, 'type': 'b'},
{'id': 5, 'type': 'a'}],
[
{'id': 1, 'type': 'a'},
{'id': 2, 'type': 'a'},
{'id': 3, 'type': 'b'},
{'id': 4, 'type': 'a'},
{'id': 5, 'type': 'b'}],
[
{'id': 1, 'type': 'a'},
{'id': 2, 'type': 'a'},
{'id': 3, 'type': 'b'},
{'id': 4, 'type': 'b'},
{'id': 5, 'type': 'a'}],
[
{'id': 1, 'type': 'a'},
{'id': 2, 'type': 'a'},
{'id': 3, 'type': 'b'},
{'id': 4, 'type': 'a'},
{'id': 5, 'type': 'b'}],
[
{'id': 1, 'type': 'a'},
{'id': 2, 'type': 'a'},
{'id': 3, 'type': 'a'},
{'id': 4, 'type': 'b'},
{'id': 5, 'type': 'b'}],
[
{'id': 1, 'type': 'a'},
{'id': 2, 'type': 'a'},
{'id': 3, 'type': 'a'},
{'id': 4, 'type': 'b'},
{'id': 5, 'type': 'b'}]]
推荐阅读
- git - 检查使用了正确版本的 python 的 git hook 会是什么样子?
- php - 今天和unix时间戳之间的天数差异..
- facebook - 使用 Facebook Audience Network SDK 在应用中投放广告
- java - Java:如何更改使用 Spring Boot yml 配置的 mongodb 端口
- javascript - DynamoDB 中的 ScanFilter 使用 BETWEEN comparisonOperator
- c++ - 如何打印二维字符数组 C++
- c# - C#从数据库中加载对象
- python - 如何检查 DataFrame 中的所有值是否在特定条件内?
- jquery - jQuery ui 手风琴子 css-counter 被覆盖
- xml - 在xml中声明变量