ken*_*ong 28 python python-3.x
为什么itertools.permutations()返回每个排列的字符或数字列表,而不是只返回一个字符串?
例如:
>>> print([x for x in itertools.permutations('1234')])
>>> [('1', '2', '3', '4'), ('1', '2', '4', '3'), ('1', '3', '2', '4') ... ]
Run Code Online (Sandbox Code Playgroud)
为什么不归还呢?
>>> ['1234', '1243', '1324' ... ]
Run Code Online (Sandbox Code Playgroud)
Sve*_*ach 45
itertools.permutations()只是以这种方式工作.它需要一个任意的iterable作为参数,并且总是返回一个产生元组的迭代器.它不(也不应该)特殊情况字符串.要获取字符串列表,您可以自己加入元组:
list(map("".join, itertools.permutations('1234')))
Run Code Online (Sandbox Code Playgroud)
gru*_*czy 15
因为它期望迭代作为参数而不知道,所以它是一个字符串.该参数在文档中描述.
http://docs.python.org/library/itertools.html#itertools.permutations
| 归档时间: |
|
| 查看次数: |
55763 次 |
| 最近记录: |