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')]

 

posted @ 2022-09-15 11:20  干了这瓶老干妈  阅读(859)  评论(0编辑  收藏  举报
Live2D