这就是我想要做的,当给出一个列表时,我想要对列表中的元素进行洗牌,并获得该列表中所有可能的元素组合。例如。给出如下所示的清单,
Ben went to the shop
所以列出并打印所有可能的组合,
shop went to the Ben
went to the Ben shop
to the went shop Ben
.
.
.
如何获得所有可能的组合并一次打印出一个字符串?
答案 0 :(得分:0)
>>> s="Ben went to the shop"
>>> L=s.split()
>>> import itertools
>>> print('\n'.join(' '.join(i) for i in list(itertools.permutations(L))))
Ben went to the shop
Ben went to shop the
Ben went the to shop
Ben went the shop to
Ben went shop to the
Ben went shop the to
Ben to went the shop
Ben to went shop the
Ben to the went shop
Ben to the shop went
Ben to shop went the
Ben to shop the went
Ben the went to shop
Ben the went shop to
Ben the to went shop
Ben the to shop went
Ben the shop went to
Ben the shop to went
Ben shop went to the
Ben shop went the to
Ben shop to went the
Ben shop to the went
Ben shop the went to
Ben shop the to went
went Ben to the shop
went Ben to shop the
went Ben the to shop
went Ben the shop to
went Ben shop to the
went Ben shop the to
went to Ben the shop
went to Ben shop the
went to the Ben shop
went to the shop Ben
went to shop Ben the
went to shop the Ben
went the Ben to shop
went the Ben shop to
went the to Ben shop
went the to shop Ben
went the shop Ben to
went the shop to Ben
went shop Ben to the
went shop Ben the to
went shop to Ben the
went shop to the Ben
went shop the Ben to
went shop the to Ben
to Ben went the shop
to Ben went shop the
to Ben the went shop
to Ben the shop went
to Ben shop went the
to Ben shop the went
to went Ben the shop
to went Ben shop the
to went the Ben shop
to went the shop Ben
to went shop Ben the
to went shop the Ben
to the Ben went shop
to the Ben shop went
to the went Ben shop
to the went shop Ben
to the shop Ben went
to the shop went Ben
to shop Ben went the
to shop Ben the went
to shop went Ben the
to shop went the Ben
to shop the Ben went
to shop the went Ben
the Ben went to shop
the Ben went shop to
the Ben to went shop
the Ben to shop went
the Ben shop went to
the Ben shop to went
the went Ben to shop
the went Ben shop to
the went to Ben shop
the went to shop Ben
the went shop Ben to
the went shop to Ben
the to Ben went shop
the to Ben shop went
the to went Ben shop
the to went shop Ben
the to shop Ben went
the to shop went Ben
the shop Ben went to
the shop Ben to went
the shop went Ben to
the shop went to Ben
the shop to Ben went
the shop to went Ben
shop Ben went to the
shop Ben went the to
shop Ben to went the
shop Ben to the went
shop Ben the went to
shop Ben the to went
shop went Ben to the
shop went Ben the to
shop went to Ben the
shop went to the Ben
shop went the Ben to
shop went the to Ben
shop to Ben went the
shop to Ben the went
shop to went Ben the
shop to went the Ben
shop to the Ben went
shop to the went Ben
shop the Ben went to
shop the Ben to went
shop the went Ben to
shop the went to Ben
shop the to Ben went
shop the to went Ben
>>>
答案 1 :(得分:0)
假设你的字符串里面有一个字符串text
。要将itertools
实现到此,您只需要通过空格.split()
,然后.join()
将它们带回空格。
如果您将来遇到itertools
遇到任何问题,请记得留意the docs!
import itertools
text = "Ben went to the shop"
text = text.split(" ")
for i in itertools.permutations(text):
print " ".join(i)
这将输出:
Ben went to the shop
Ben went to shop the
Ben went the to shop
Ben went the shop to
Ben went shop to the
Ben went shop the to
Ben to went the shop
Ben to went shop the
Ben to the went shop
Ben to the shop went
Ben to shop went the
Ben to shop the went
Ben the went to shop
Ben the went shop to
Ben the to went shop
Ben the to shop went
Ben the shop went to
Ben the shop to went
Ben shop went to the
Ben shop went the to
Ben shop to went the
Ben shop to the went
Ben shop the went to
Ben shop the to went
went Ben to the shop
went Ben to shop the
went Ben the to shop
went Ben the shop to
went Ben shop to the
went Ben shop the to
went to Ben the shop
went to Ben shop the
went to the Ben shop
went to the shop Ben
went to shop Ben the
went to shop the Ben
went the Ben to shop
went the Ben shop to
went the to Ben shop
went the to shop Ben
went the shop Ben to
went the shop to Ben
went shop Ben to the
went shop Ben the to
went shop to Ben the
went shop to the Ben
went shop the Ben to
went shop the to Ben
to Ben went the shop
to Ben went shop the
to Ben the went shop
to Ben the shop went
to Ben shop went the
to Ben shop the went
to went Ben the shop
to went Ben shop the
to went the Ben shop
to went the shop Ben
to went shop Ben the
to went shop the Ben
to the Ben went shop
to the Ben shop went
to the went Ben shop
to the went shop Ben
to the shop Ben went
to the shop went Ben
to shop Ben went the
to shop Ben the went
to shop went Ben the
to shop went the Ben
to shop the Ben went
to shop the went Ben
the Ben went to shop
the Ben went shop to
the Ben to went shop
the Ben to shop went
the Ben shop went to
the Ben shop to went
the went Ben to shop
the went Ben shop to
the went to Ben shop
the went to shop Ben
the went shop Ben to
the went shop to Ben
the to Ben went shop
the to Ben shop went
the to went Ben shop
the to went shop Ben
the to shop Ben went
the to shop went Ben
the shop Ben went to
the shop Ben to went
the shop went Ben to
the shop went to Ben
the shop to Ben went
the shop to went Ben
shop Ben went to the
shop Ben went the to
shop Ben to went the
shop Ben to the went
shop Ben the went to
shop Ben the to went
shop went Ben to the
shop went Ben the to
shop went to Ben the
shop went to the Ben
shop went the Ben to
shop went the to Ben
shop to Ben went the
shop to Ben the went
shop to went Ben the
shop to went the Ben
shop to the Ben went
shop to the went Ben
shop the Ben went to
shop the Ben to went
shop the went Ben to
shop the went to Ben
shop the to Ben went
shop the to went Ben
答案 2 :(得分:0)
应该这样做:
string = "Ben went to the shop".split()
res = []
while len(res) < factorial(len(string)):
temp = create_random(string)
if temp not in res: res.append(temp)
print(res)