我有以下几乎对我有用的python代码(我很接近!)。我从莎士比亚的一部戏剧中打开了一个文本文件:原始文本文件:
“但是柔和什么透过窗户的光线会打破
它是东方,朱丽叶是太阳
升起阳光,杀死令人羡慕的月亮
谁已经生病,因悲伤而苍白”
我编写的代码的结果是:
[“ Arise”,“ But”,“ It”,“ Juliet”,“ Who”,“已经”,“ and”,“ and”,“ and”,“ breaks”,“ east”,“ en羡慕”,“公平'',``悲痛'',``是'',``是'',``是'',``杀'',``轻'',``月亮'',``苍白'',``病'',``软'',``太阳'',``太阳'' ,“ the”,“ the”,“ the”,“ through”,“ what”,“ window”,“ with”,“ yonder”]
所以这几乎是我想要的:它已经在列表中以所需的方式排序,但是如何删除重复的单词呢?我正在尝试创建一个新的ResultsList并将单词附加到它上面,但是它为我提供了以上结果,而又没有消除重复的单词。如果我“打印ResultsList”,它只会输出大量的单词。他们的方式我现在已经很接近了,但是我想摆脱多余的“和”,“是”,“ sun”和“ the” ....我想保持简单并使用append(),但是我不确定如何使它正常工作。我不想对代码做任何疯狂的事情。为了删除重复的单词,我的代码中缺少什么简单的东西?
fname = raw_input("Enter file name: ")
fhand = open(fname)
NewList = list() #create new list
ResultList = list() #create new results list I want to append words to
for line in fhand:
line.rstrip() #strip white space
words = line.split() #split lines of words and make list
NewList.extend(words) #make the list from 4 lists to 1 list
for word in line.split(): #for each word in line.split()
if words not in line.split(): #if a word isn't in line.split
NewList.sort() #sort it
ResultList.append(words) #append it, but this doesn't work.
print NewList
#print ResultList (doesn't work the way I want it to)
您的代码确实有几个逻辑错误。我修复了它们,希望对您有所帮助。
fname = "stuff.txt"
fhand = open(fname)
AllWords = list() #create new list
ResultList = list() #create new results list I want to append words to
for line in fhand:
line.rstrip() #strip white space
words = line.split() #split lines of words and make list
AllWords.extend(words) #make the list from 4 lists to 1 list
AllWords.sort() #sort list
for word in AllWords: #for each word in line.split()
if word not in ResultList: #if a word isn't in line.split
ResultList.append(word) #append it.
print(ResultList)
在Python 3.4上测试,不导入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句