【发布时间】:2013-04-09 01:05:49
【问题描述】:
我需要从一个文本文件创建一个单词列表。该列表将用于刽子手代码中,需要从列表中排除以下内容:
- 重复的单词
- 少于 5 个字母的单词
- 包含 'xx' 作为子字符串的单词
- 包含大写字母的单词
然后需要将单词列表输出到文件中,以便每个单词都出现在自己的行中。 程序还需要输出最终列表中的单词数。
这是我所拥有的,但它无法正常工作。
def MakeWordList():
infile=open(('possible.rtf'),'r')
whole = infile.readlines()
infile.close()
L=[]
for line in whole:
word= line.split(' ')
if word not in L:
L.append(word)
if len(word) in range(5,100):
L.append(word)
if not word.endswith('xx'):
L.append(word)
if word == word.lower():
L.append(word)
print L
MakeWordList()
【问题讨论】:
-
怎么不能正常工作?您预计会发生什么,以及真正会发生什么?
-
5个字母以下的单词不带走,大写字母的单词保留。