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

后端 未结 8 1635
渐次进展
渐次进展 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:43

    This should do the job:

    fname = input("Enter file name: ")
    fh = open(fname)
    lst = list()
    for line in fh:
        line = line.rstrip()
        words = line.split()
        for word in words:
            if word not in lst:
                lst.append(word)
    lst.sort()
    print(lst)
    

提交回复
热议问题