简体   繁体   中英

Attempting to remove repeated words in a list python

I'm trying to remove repeated words in a list where it saves the location and word in a file but it doesn't save the word which occurs after the repeated word. Can someone tell me what's wrong with it?

sen = input("Input a sentence")
sen1 = sen.lower()
sen2 = sen1.split()
sen4 = sen2
f = open("newfile.txt", "w+")
for words in sen2:
    print(words)
    ok = sen1.count(words)
    print(ok)
    sent = sen4.index(words)
    print(sent)
    f.write(str(sent))
    f.write(str(words))
    if ok > 1 :
        while ok > 0:
            sen2.remove(words)
            ok = ok-1
            if ok == 0:
                break
f.close()

You are making a common mistake, modifying a list while you are looping over its items.

Inside the loop for words in sen2: do sometimes execute sen2.remove(words) , which modifies the list sen2 . Strange things happened when you do this.

To avoid this, make a deep copy of the list sen2 with sen2copy = sen2[:] , and loop over one of them and modify the other one. You could do this with

sen2copy = sen2[:]
for words in sen2copy:

or, if you want to be brief,

for words in sen2[:]:

If you don't understand the notation, sen2[:] is a slice of sen2 from the beginning to the end. In other words, it copies each item in sen2 to the new list. If you leave out the brackets and colon you just copy a reference to the entire list, which is not what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM