欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python中的itertools.permutations

程序员文章站 2024-02-12 13:22:52
...

通俗地讲,就是返回可迭代对象的所有数学全排列方式。

Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import permutations
>>> permutations(['a', 'b', 'c'])
<itertools.permutations object at 0x7ff7b1411890>
>>> for item in permutations(['a', 'b', 'c']):
...     print item
...
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
>>> for i in itertools.permutations('123', 2):
...     print i
...
('1', '2')
('1', '3')
('2', '1')
('2', '3')
('3', '1')
('3', '2')
>>> itertools.permutations('123', 2)
<itertools.permutations object at 0x7f5addcca8f0>