在 Python 中生成排列单词列表的子集

发布于 2024-10-07 10:35:43 字数 446 浏览 7 评论 0原文

我有一个单词列表,我需要生成这些单词的所有可能的排列,但有一个警告。

我目前使用以下代码:

from itertools import permutations

wordlist = ["word1", "word2", "word3"]

for perm in permutations(wordlist):
    print "".join(perm)

给出输出:

word1word2word3
word1word3word2
...
word3word2word1

但是我还需要它来打印这些单词的子集,例如:

word1    
word1word2
word2word1
...

但我完全不知道如何做到这一点。 我从哪里开始呢?我应该读什么?

I have a list of words and I need to generate all possible permutations of these, with one caveat.

I currently use the following code:

from itertools import permutations

wordlist = ["word1", "word2", "word3"]

for perm in permutations(wordlist):
    print "".join(perm)

which gives the output:

word1word2word3
word1word3word2
...
word3word2word1

However I also need it to print subsets these words, such as:

word1    
word1word2
word2word1
...

But I haven't the slightest idea how to do this.
Where do I begin? What should I read?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-10-14 10:35:43

编辑:

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

Edited:

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)

Edit: output:

word1
word2
word3
word1word2
word1word3
word2word1
word2word3
word3word1
word3word2
word1word2word3
word1word3word2
word2word1word3
word2word3word1
word3word1word2
word3word2word1
星星的轨迹 2024-10-14 10:35:43

这是一个更完整的文件 I/O 实现。
谢谢史蒂夫,我根据你的回答。
与其他帖子不同,此代码是为 Python 3.3.4 编写的

from itertools import permutations
import os

# GET FILE
script_dir = os.path.dirname(os.path.realpath(__file__))
wordlist_rel_path = "wordlist.txt"
wordlist_abs_file_path = os.path.join(script_dir, wordlist_rel_path)

# READ WORD LIST FROM FILE
word_list = []
print ("\ninput file is:", wordlist_abs_file_path,"\n")
with open(wordlist_abs_file_path) as wordlist:
     for line in wordlist:
         word_list.append(line.rstrip())

# PRINT INPUT LIST
print ("input list contains:")
print(word_list,"\n")

# GENERATE POWERSET
powerset_list = []
print ("output list is:")
for n in range(1, len(word_list)+1):
     for perm in permutations(word_list, n):
         powerset_list.append( "".join(perm) )
print(powerset_list)

# WRITE LIST TO FILE
powerset_rel_path = "powerset.txt"
powerset_abs_file_path = os.path.join(script_dir, powerset_rel_path)
powerset_abs_file = open(powerset_abs_file_path, 'w')
for item in powerset_list:
     powerset_abs_file.write("%s\n" % item)
powerset_abs_file.close()

Here is a more complete implementation with file I/O.
Thanks Steve, I based myself on your answer.
This code, unlike other posts, was written for Python 3.3.4

from itertools import permutations
import os

# GET FILE
script_dir = os.path.dirname(os.path.realpath(__file__))
wordlist_rel_path = "wordlist.txt"
wordlist_abs_file_path = os.path.join(script_dir, wordlist_rel_path)

# READ WORD LIST FROM FILE
word_list = []
print ("\ninput file is:", wordlist_abs_file_path,"\n")
with open(wordlist_abs_file_path) as wordlist:
     for line in wordlist:
         word_list.append(line.rstrip())

# PRINT INPUT LIST
print ("input list contains:")
print(word_list,"\n")

# GENERATE POWERSET
powerset_list = []
print ("output list is:")
for n in range(1, len(word_list)+1):
     for perm in permutations(word_list, n):
         powerset_list.append( "".join(perm) )
print(powerset_list)

# WRITE LIST TO FILE
powerset_rel_path = "powerset.txt"
powerset_abs_file_path = os.path.join(script_dir, powerset_rel_path)
powerset_abs_file = open(powerset_abs_file_path, 'w')
for item in powerset_list:
     powerset_abs_file.write("%s\n" % item)
powerset_abs_file.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文