改进单词组合脚本

时间:2012-11-06 02:11:00

标签: python

任何使这更好或更简单的方法?我知道它会生成很多单词,当你尝试在一个句子上组合超过4行时,它看起来不应该是这样。

infile = open('Wordlist.txt.txt','r')
wordlist = []
for line in infile:
    wordlist.append(line.strip())
infile.close()
outfile = open('output.txt','w')
for word1 in wordlist:
    for word2 in wordlist:
        out = '%s %s' %(word1,word2)
        #feel free to #comment one of these two lines to not output to file or screen
        print out
        outfile.write(out + '\n')

outfile.close()

2 个答案:

答案 0 :(得分:4)

使用itertools.product

with open('Wordlist.txt.txt') as infile:
    words = [line.strip() for line in infile]

with open('output.txt', 'w') as outfile:
    for word1, word2 in itertools.product(words, repeat=2):
        outfile.write("%s %s\n" %(word1, word2))

答案 1 :(得分:1)

如果您的infile中的每一行都包含两个单词,您可以考虑:

from itertools import product

with open('Wordlist.txt.txt','r') as infile:
   wordlist=infile.readlines()

with open('output','w') as ofile:
   ofile.write('\n'.join(map(product, [line.strip().split() for line in wordlist])))