Itertools排列

时间:2017-11-05 13:37:42

标签: python python-3.x itertools

我有一个列表说x = [1,2,3,4,5]并希望查看此列表的不同排列,一次取两个数字。

x=[1,2,3,4,5] 
from itertools import permutations
y=list(i for i in permutations(x,2) if i[0]<i[1])
print(y)

输出:[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]

但我也希望输出中有[(1,1),(2,2),(3,3),(4,4),(5,5)]。如何纠正这个?

1 个答案:

答案 0 :(得分:4)

您想要combinations_with_replacement()代替:

{$my_var} <- 1.7 way