python List中元素两两组合
代码:
import itertools aa = ['a', 'b', 'c'] bb = list(itertools.permutations(aa, 2)) print(bb) print("######################") cc = list(itertools.combinations(aa, 2)) print(cc) print("######################") dd = list(itertools.combinations(aa, 3)) print(dd) print(cc+dd)
运行结果:
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')] ###################### [('a', 'b'), ('a', 'c'), ('b', 'c')] ###################### [('a', 'b', 'c')] [('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]