如何从python列表中删除重复的单词而不使用集合? [英] How do I remove duplicate words from a list in python without using sets?

查看:369
本文介绍了如何从python列表中删除重复的单词而不使用集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下python代码几乎适用于我(我很接近!)。我有一个莎士比亚戏剧的文本文件,我打开:
原始文本文件:



但是,通过yonder窗口打破什么光线



这是东部和朱丽叶是太阳



升起公平的阳光,杀死羡慕的月亮



谁已经生病了,脸色苍白



而且,我给我的代码的结果是这样的:



['Arise','But','It','Juliet','Who','已经','和','和','和',$ b $ 呃,en,en,en,en,en '''''''''''''''''''''''''''''''' ,'yonder']



所以这几乎是我想要的:它已经在列表中按照我想要的方式排序,但是如何删除重复的单词?我试图创建一个新的结果列表并附加单词,但它给了我上述结果,而不会摆脱重复的单词。如果我打印结果列表,它只会转储一吨的字。他们现在的方式现在很近,但是我想摆脱额外的和,是,太阳和的....我想保持简单,并使用append(),但是我不知道如何让它上班。我不想对代码做任何事情疯狂。我的代码缺少什么简单的东西,以便删除重复的单词?

  fname = raw_input(输入文件名: )
fhand = open(fname)
NewList = list()#create新列表
ResultList = list()#create新结果列表我想要添加到

for fhand:
line.rstrip()#strip white space
words = line.split()#split行词和make列表
NewList.extend(words)#将列表从4个列表变为1个列表

for line.split()中的每个单词:#for line.split()中的每个单词
如果单词不在line.split()中: #if一个字不在line.split
NewList.sort()#sort it
ResultList.append(words)#append it,但这不起作用。


打印NewList
#print ResultList(不能按照我想要的方式)


解决方案

你的代码确实有一个逻辑错误。我修正了他们,希望它有帮助。

  fname =stuff.txt
fhand = open(fname)
AllWords = list()#create新列表
ResultList = list()#create新结果列表我想要添加单词到

for fhand:
line .rstrip()#strip白色空间
words = line.split()#split的单词和列表
AllWords.extend(words)#将列表从4个列表中删除到1个列表

AllWords.sort()#sort列表

在所有词中的单词:#for line.split()中的每个单词
如果单词不在ResultList中:#if一个单词不在line.split
ResultList.append(word)#append它。


print(ResultList)

不导入。


I have the following python code which almost works for me (I'm SO close!). I have text file from one Shakespeare's plays that I'm opening: Original text file:

"But soft what light through yonder window breaks

It is the east and Juliet is the sun

Arise fair sun and kill the envious moon

Who is already sick and pale with grief"

And the result of the code I worte gives me is this:

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']

So this is almost what I want: It's already in a list sorted the way I want it, but how do I remove the duplicate words? I'm trying to create a new ResultsList and append the words to it, but it gives me the above result without getting rid of the duplicate words. If I "print ResultsList" it just dumps a ton of words out. They way I have it now is close, but I want to get rid of the extra "and's", "is's", "sun's" and "the's".... I want to keep it simple and use append(), but I'm not sure how I can get it to work. I don't want to do anything crazy with the code. What simple thing am I missing from my code inorder to remove the duplicate words?

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)

解决方案

You did have a couple logic error with your code. I fixed them, hope it helps.

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)

Tested on Python 3.4, no importing.

这篇关于如何从python列表中删除重复的单词而不使用集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆