<分区>
如何遍历列表中的所有对组合,例如:
list = [1,2,3,4]
输出:
1,2
1,3
1,4
2,3
2,4
3,4
谢谢!
<分区>
如何遍历列表中的所有对组合,例如:
list = [1,2,3,4]
输出:
1,2
1,3
1,4
2,3
2,4
3,4
谢谢!
最佳答案
>>> import itertools
>>> lst = [1,2,3,4]
>>> for x in itertools.combinations(lst, 2):
... print(x)
...
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
顺便说一句,不要使用 list
作为变量名。它隐藏了内置函数/类型 list
。
关于python - 遍历 Python 列表中的每两个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27017439/