IndexError:列表赋值索引超出范围,删除

IndexError: list assignment index out of range, deletion

提问人:n0te 提问时间:8/5/2023 更新时间:8/6/2023 访问量:35

问:

我正在尝试制作一个程序,该程序循环遍历列表并删除列表其余部分具有相似标题的项目。headlines

# Loop through headlines and remove over 50% similar ones
headlines = listHeadlines()
# headlines.append('Our plan is working says Hunt, as Bank raises interest rate to 5.25%')
print(len(headlines), headlines)
headlines_copy = list(headlines)
for headline in headlines_copy:
    for h in headlines_copy:
        if h == headline:
            pass
        elif areStringsSimilar(h, headline):
            del headlines[headlines_copy.index(headline)]
            break  # Exit this loop and move back to other because headline has been deleted from list.

print(len(headlines), headlines)

第一个工作并打印,但随后出现错误:print(len(headlines), headlines)1248 [list]

Traceback (most recent call last):
  File "/Users/[path]/main.py", line 95, in <module>
    del headlines[headlines_copy.index(headline)]
        ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: list assignment index out of range

Process finished with exit code 1
python list for-loop range del

评论

0赞 sahasrara62 8/5/2023
为什么不呢?使用 while 循环从列表中删除list(set(headlines))
0赞 Bill Lynch 8/5/2023
如果您的列表是 ,其中 ,您希望结果是 或 ?["a", "b", "c"]areStringsSimilar("a", "b") == true["a","c"]["b", "c"]["c"]
0赞 Bill Lynch 8/5/2023
@sahasrara62:它们用于确定相似性。areStringsSimilar()

答:

0赞 Bill Lynch 8/6/2023 #1

https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

list.remove(x)

从列表中删除其值等于 x 的第一项。如果没有此类项,则引发 ValueError。

所以与其说,不如做.del headlines[headlines_copy.index(headline)]headlines.remove(headline)

0赞 sahasrara62 8/6/2023 #2
index = 0
headlines = listHeadlines()
print(len(headlines), headlines)
while index < len(headlines):
    for i, h in enumerate(headlines):
        if i == index:
            pass
        elif areStringsSimilar(h, headline):
            del headlines[i]
            index -= 1
            break  
    else:
       index += 1

print(len(headlines), headlines)
0赞 Luatic 8/6/2023 #3

为什么不附加你想保留的标题,而不是删除你不想要的标题:

headlines = listHeadlines()
deduplicated = []
for candidate in headlines:
    if not any(map(lambda kept: areStringsSimilar(candidate, kept), deduplicated)):
        deduplicated.append(candidate)
print(deduplicated)

评论

0赞 n0te 8/6/2023
谢谢。我不想将标题附加到新列表中,因为我不知道如何在使用嵌套循环时多次附加相同的标题。您的解决方案似乎避免了这种情况,所以谢谢。