python – itertools:排列的笛卡尔乘积
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – itertools:排列的笛卡尔乘积,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1174字,纯文字阅读大概需要2分钟。
内容图文
![python – itertools:排列的笛卡尔乘积](/upload/InfoBanner/zyjiaocheng/760/e213f34283384ddc9ff52014ee306c38.jpg)
使用pythons itertools,我想在一堆列表的所有排列的外积上创建一个迭代器.一个明确的例子:
import itertools
A = [1,2,3]
B = [4,5]
C = [6,7]
for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)):
print x
虽然这有效,但我想将其推广到任意列表列表.我试过了:
for x in itertools.product(map(itertools.permutations,[A,B,C])):
print x
但它并没有按照我的意图行事.预期的产出是:
((1, 2, 3), (4, 5), (6, 7))
((1, 2, 3), (4, 5), (7, 6))
((1, 2, 3), (5, 4), (6, 7))
((1, 2, 3), (5, 4), (7, 6))
((1, 3, 2), (4, 5), (6, 7))
((1, 3, 2), (4, 5), (7, 6))
((1, 3, 2), (5, 4), (6, 7))
((1, 3, 2), (5, 4), (7, 6))
((2, 1, 3), (4, 5), (6, 7))
((2, 1, 3), (4, 5), (7, 6))
((2, 1, 3), (5, 4), (6, 7))
((2, 1, 3), (5, 4), (7, 6))
((2, 3, 1), (4, 5), (6, 7))
((2, 3, 1), (4, 5), (7, 6))
((2, 3, 1), (5, 4), (6, 7))
((2, 3, 1), (5, 4), (7, 6))
((3, 1, 2), (4, 5), (6, 7))
((3, 1, 2), (4, 5), (7, 6))
((3, 1, 2), (5, 4), (6, 7))
((3, 1, 2), (5, 4), (7, 6))
((3, 2, 1), (4, 5), (6, 7))
((3, 2, 1), (4, 5), (7, 6))
((3, 2, 1), (5, 4), (6, 7))
((3, 2, 1), (5, 4), (7, 6))
解决方法:
您错过了*将列表解压缩为3个参数
itertools.product(*map(itertools.permutations,[A,B,C]))
内容总结
以上是互联网集市为您收集整理的python – itertools:排列的笛卡尔乘积全部内容,希望文章能够帮你解决python – itertools:排列的笛卡尔乘积所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。