python 列表的排列组合

2019-09-30 18:30:17 浏览数 (2)

permutations/combinations/combinations_with_replacement

代码语言:javascript复制
   items = ['a', 'b', 'c']
 from itertools import permutations
 for p in permutations(items):
 ...     print(p)
 ...
 ('a', 'b', 'c')
 ('a', 'c', 'b')
 ('b', 'a', 'c')
 ('b', 'c', 'a')
 ('c', 'a', 'b')
 ('c', 'b', 'a')
 
 
 
   for p in permutations(items, 2):
 ...     print(p)
 ...
 ('a', 'b')
 ('a', 'c')
 ('b', 'a')
 ('b', 'c')
 ('c', 'a')
 ('c', 'b')
 
 
 
   from itertools import combinations
 for c in combinations(items, 3):
 ...     print(c)
 ...
 ('a', 'b', 'c')
 
 
 
   for c in combinations(items, 2):
 ...     print(c)
 ...
 ('a', 'b')
 ('a', 'c')
 ('b', 'c')
 
 
 
   for c in combinations(items, 1):
 ...     print(c)
 ...
 ('a',)
 ('b',)
 ('c',)
 
 
 
   for c in combinations_with_replacement(items, 3):
 ...     print(c)
 ...
 ('a', 'a', 'a')
 ('a', 'a', 'b')
 ('a', 'a', 'c')
 ('a', 'b', 'b')
 ('a', 'b', 'c')
 ('a', 'c', 'c')
 ('b', 'b', 'b')
 ('b', 'b', 'c')
 ('b', 'c', 'c')
 ('c', 'c', 'c')
 
 
 

0 人点赞