简体   繁体   English

尝试删除列表python中的重复单词

[英]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 . for words in sen2:循环for words in sen2:有时要执行sen2.remove(words) ,它会修改列表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. 为避免这种情况, sen2使用sen2copy = sen2[:]对列表sen2进行深层复制,然后遍历其中一个并修改另一个。 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. 如果您不理解该符号,则sen2[:]sen2切片。 In other words, it copies each item in sen2 to the new list. 换句话说,它将sen2中的每个项目sen2到新列表中。 If you leave out the brackets and colon you just copy a reference to the entire list, which is not what you want. 如果省略括号和冒号,则只需将引用复制到整个列表,这不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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