python 对可迭代对象的某些字段进行排列组合

在使用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就是我们想要的结果啦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值