【2017-09-26】迭代器与生成器(四)

2017-09-27  本文已影响0人  小蜗牛的成长
#组合
>>> from itertools import permutations
>>> data=[1,2,3]
>>> for p in permutations(data):
    print(p)
    
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
#>>> 指定返回结果中每种排列的长度(元素个数)
>>> for p in permutations(data,2):
    print(p)

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
>>> 

组合

>>> from itertools import combinations
>>> data=[1,2,3]
>>> for c in combinations(data, 3):
    print(c)

(1, 2, 3)
>>> 
>>> for c in combinations(data,2):
    print(c)
    
(1, 2)
(1, 3)
(2, 3)
>>> 
>>> from itertools import chain
>>> a=[1,2,3,4]
>>> b=set('x','y','z')
>>> for i in chain(a,b):
    print(i)
上一篇 下一篇

猜你喜欢

热点阅读