您的位置:首页 > 编程语言 > Python开发

Python中的itertools.permutations(关键词:itertools/permutations)

2018-02-25 22:25 375 查看
通俗地讲,就是返回可迭代对象的所有数学全排列方式。

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>


参考文献:

1. Python中itertools模块用法详解 - 脚本之家

2. itertools.permutations - Python标准库
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息