python:在字符串中随机播放字符以获取所有可能的字符串组合[重复]

时间:2021-05-21 21:37:02

This question already has an answer here:

这个问题在这里已有答案:

just looking for a script in Python which receives some string and returns all possible strings made up of all the possible combinations of the chars in the original string...

只是在Python中寻找一个脚本,它接收一些字符串并返回所有可能的字符串,这些字符串由原始字符串中字符的所有可能组合组成......

I've found scripts to shuffle randomly the chars in a string, but they only return one randome combination, and what I'm looking for is all the possible combinations...

我发现脚本随机乱换字符串中的字符,但它们只返回一个randome组合,而我正在寻找的是所有可能的组合......

Say, for example:

比方说,例如:

script.py "abc"
abc
acb
bac
bca
cab
cba

Thanks!

谢谢!

2 个解决方案

#1


10  

itertools.permutations

和itertools.permutations

>>> import itertools
>>> import pprint
>>> pprint.pprint(list(itertools.permutations("spam")))
[('s', 'p', 'a', 'm'),
 ('s', 'p', 'm', 'a'),
 ('s', 'a', 'p', 'm'),
 ('s', 'a', 'm', 'p'),
 ('s', 'm', 'p', 'a'),
 ('s', 'm', 'a', 'p'),
 ('p', 's', 'a', 'm'),
 ('p', 's', 'm', 'a'),
 ('p', 'a', 's', 'm'),
 ('p', 'a', 'm', 's'),
 ('p', 'm', 's', 'a'),
 ('p', 'm', 'a', 's'),
 ('a', 's', 'p', 'm'),
 ('a', 's', 'm', 'p'),
 ('a', 'p', 's', 'm'),
 ('a', 'p', 'm', 's'),
 ('a', 'm', 's', 'p'),
 ('a', 'm', 'p', 's'),
 ('m', 's', 'p', 'a'),
 ('m', 's', 'a', 'p'),
 ('m', 'p', 's', 'a'),
 ('m', 'p', 'a', 's'),
 ('m', 'a', 's', 'p'),
 ('m', 'a', 'p', 's')]

(The pprint is just there to make the output look neater.) Or, if you prefer,

(pprint就是为了使输出看起来更整洁。)或者,如果你愿意,

>>> list(map("".join, itertools.permutations("spam")))
['spam', 'spma', 'sapm', 'samp', 'smpa', 'smap', 'psam', 'psma', 'pasm', 'pams', 'pmsa', 'pmas', 'aspm', 'asmp', 'apsm', 'apms', 'amsp', 'amps', 'mspa', 'msap', 'mpsa', 'mpas', 'masp', 'maps']

#2


3  

itertools.permutations does that.

itertools.permutations那样做。

>>> import itertools
>>> for s in itertools.permutations('banana'):
...     print ''.join(s)
... 
banana
banaan
bannaa
bannaa
# many, many more...

#1


10  

itertools.permutations

和itertools.permutations

>>> import itertools
>>> import pprint
>>> pprint.pprint(list(itertools.permutations("spam")))
[('s', 'p', 'a', 'm'),
 ('s', 'p', 'm', 'a'),
 ('s', 'a', 'p', 'm'),
 ('s', 'a', 'm', 'p'),
 ('s', 'm', 'p', 'a'),
 ('s', 'm', 'a', 'p'),
 ('p', 's', 'a', 'm'),
 ('p', 's', 'm', 'a'),
 ('p', 'a', 's', 'm'),
 ('p', 'a', 'm', 's'),
 ('p', 'm', 's', 'a'),
 ('p', 'm', 'a', 's'),
 ('a', 's', 'p', 'm'),
 ('a', 's', 'm', 'p'),
 ('a', 'p', 's', 'm'),
 ('a', 'p', 'm', 's'),
 ('a', 'm', 's', 'p'),
 ('a', 'm', 'p', 's'),
 ('m', 's', 'p', 'a'),
 ('m', 's', 'a', 'p'),
 ('m', 'p', 's', 'a'),
 ('m', 'p', 'a', 's'),
 ('m', 'a', 's', 'p'),
 ('m', 'a', 'p', 's')]

(The pprint is just there to make the output look neater.) Or, if you prefer,

(pprint就是为了使输出看起来更整洁。)或者,如果你愿意,

>>> list(map("".join, itertools.permutations("spam")))
['spam', 'spma', 'sapm', 'samp', 'smpa', 'smap', 'psam', 'psma', 'pasm', 'pams', 'pmsa', 'pmas', 'aspm', 'asmp', 'apsm', 'apms', 'amsp', 'amps', 'mspa', 'msap', 'mpsa', 'mpas', 'masp', 'maps']

#2


3  

itertools.permutations does that.

itertools.permutations那样做。

>>> import itertools
>>> for s in itertools.permutations('banana'):
...     print ''.join(s)
... 
banana
banaan
bannaa
bannaa
# many, many more...