在使用python的过程中,可能会遇到需要对list或者dict中的其中几个字段进行排列组合的情况,比如对于下面的example,想要得到最终res1-res6的结果,该如何实现呢?
example = {"name":"Alex", "age":[18,19,20],"height":["175","180"]}
res1 = {"name":"Alex", "age":18,"height":"175"}
res2 = {"name":"Alex", "age":18,"height":"180"}
res3 = {"name":"Alex", "age":19,"height":"175"}
res4 = {"name":"Alex", "age":19,"height":"180"}
res5 = {"name":"Alex", "age":20,"height":"175"}
res6 = {"name":"Alex", "age":20,"height":"180"}
其实python在itertools包中是提供了和排列组合相关的函数的,如下代码
from itertools import combinations,permutations
a=[1,2,3,4]
b=[2,4,6,8]
c = list(combinations(a,2))
d = list(permutations(b,3))
print(c)
print(d)
运算结果:
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
[(2, 4, 6), (2, 4, 8), (2, 6, 4), (2, 6, 8), (2, 8, 4), (2, 8, 6), (4, 2, 6), (4, 2, 8), (4, 6, 2), (4, 6, 8), (4, 8, 2), (4, 8, 6), (6, 2, 4), (6, 2, 8), (6, 4, 2), (6, 4, 8), (6, 8, 2), (6, 8, 4), (8, 2, 4), (8, 2, 6), (8, 4, 2), (8, 4, 6), (8, 6, 2), (8, 6, 4)]
可以看到combinations是对列表中要求的元素个数进行无序的排列组合,permutations则是对列表中要求的元素个数进行有序的排列组合,然而仅使用这两个方法得到的结果将列表中其他元素舍弃了,这不是我们想要的结果,其实我们可以使用product函数自己实现
# 原始的数据
data = {"name":"Alex", "age":[18,19,20],"height":["175","180"]}
# 想要进行排列组合的key
product_keys = ["age", "heght"]
product_info = {}
for k, v in data.items():
if k in PRODUCT_KEYS:
# 以防想要进行排列组合的键不可迭代,统一转成list类型
product_info[k] = v if isinstance(v, list) else [v]
# 对需要迭代的字典解包,取出里面的元素
keys, values = zip(*product_info.items())
for row in itertools.product(*values):
yield {**data, **dict(zip(keys, row))}
最终的data就是我们想要的结果啦~