我有一个单词列表,我需要生成所有这些可能的排列,但需要注意一点.
我目前使用以下代码:
from itertools import permutations
wordlist = ["word1", "word2", "word3"]
for perm in permutations(wordlist):
print "".join(perm)
它给出了输出:
word1word2word3
word1word3word2
...
word3word2word1
但是我还需要它来打印这些单词的子集,例如:
word1
word1word2
word2word1
...
但我对如何做到这一点没有丝毫想法.
我该从哪里开始?我该怎么读?
解决方法:
编辑:
from itertools import permutations
xlist = ["word1", "word2", "word3"]
for n in range(1, len(xlist)+1):
for perm in permutations(xlist, n):
print "".join(perm)
编辑:输出:
word1
word2
word3
word1word2
word1word3
word2word1
word2word3
word3word1
word3word2
word1word2word3
word1word3word2
word2word1word3
word2word3word1
word3word1word2
word3word2word1