numList =[1,3,5,7,9]from itertools import combinations
for i inrange(1,len(numList)+1):# xrange will return the values 1,2,3,4 in this loopprint(list(combinations(numList, i)))
输出:
[('1',),('3',),('5',),('7',),('9',)][('1','3'),('1','5'),('1','7'),('1','9'),('3','5'),('3','7'),('3','9'),('5','7'),('5','9'),('7','9')][('1','3','5'),('1','3','7'),('1','3','9'),('1','5','7'),('1','5','9'),('1','7','9'),('3','5','7'),('3','5','9'),('3','7','9'),('5','7','9')][('1','3','5','7'),('1','3','5','9'),('1','3','7','9'),('1','5','7','9'),('3','5','7','9')][('1','3','5','7','9')]
[[[x,y]for x in list1]for y in list2]
输出
[[[1,5],[2,5],[3,5],[4,5],[5,5]],[[1,6],[2,6],[3,6],[4,6],[5,6]],[[1,7],[2,7],[3,7],[4,7],[5,7]],[[1,8],[2,8],[3,8],[4,8],[5,8]],[[1,9],[2,9],[3,9],[4,9],[5,9]]][[x,y]for x in list1 for y in list2][[1,5],[1,6],[1,7],[1,8],[1,9],[2,5],[2,6],[2,7],[2,8],[2,9],[3,5],[3,6],[3,7],[3,8],[3,9],[4,5],[4,6],[4,7],[4,8],[4,9],[5,5],[5,6],[5,7],[5,8],[5,9]]
repeat第一个列表,permutate第二个列表和zip它们一起
from itertools import permutations, repeat
a =[1,2,3]
b =[4,5,6]print(list(list(zip(r, p))for(r, p)inzip(repeat(a), permutations(b))))[[(1,4),(2,5),(3,6)],[(1,4),(2,6),(3,5)],[(1,5),(2,4),(3,6)],[(1,5),(2,6),(3,4)],[(1,6),(2,4),(3,5)],[(1,6),(2,5),(3,4)]]