How do I remove duplicate words from a list in python without using sets?

后端 未结 8 1605
渐次进展
渐次进展 2021-02-11 04:24

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:

\"Bu

8条回答
  •  无人及你
    2021-02-11 04:34

    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.

提交回复
热议问题