.remove(word) 在检查单词后立即返回 “不在列表中”。

huangapple go评论68阅读模式
英文:

.remove(word) gives "not in the list" right after a check for that word

问题

我之前已经运行过这段代码,我相信错误不是由于其他代码部分失败引起的。我正在编写一个解析器,它会迭代遍历数据框中的每一行,并将一个字符串解析成输出数据框中的多个列。最近我在一个for循环内添加了一些新的行:

for word in words:
    word = word.replace("-", " ")
    if word in measurement_units:
        measurement = word
        words.remove(word)

唯一添加的内容是if语句以及之后的内容。现在,我的代码出现了错误:

    words.remove(word)
ValueError: list.remove(x): x not in list

这个错误不常见,它在执行了数十万行之后才出现,所以它不可能是完全微不足道的问题 - 显然某种类型的字符串会触发它。我是一个完全的初学者,所以我已经向bing AI提问,它要我按照以下方式写:

words = row.split(" ")
new_words = []
for word in words:
    word = word.replace("-", " ")
    if word not in measurement_units:
        new_words.append(word)
    else:
        measurement = word
    words = new_words

这个方法可以工作,但为什么我需要这样做呢?在我看来,这个问题本来不应该发生 - 我们知道这个词存在,因为“if”语句已经隐式检查了它是否为“”(空字符串),并且在if语句和words.remove之间没有进行删除操作。怎么回事?

没有太多东西可以测试,因为在一个两行的代码片段中你只能写这么多测试,而且因为我有超过一百万行数据,打印每一行或每个单词都不可行。

英文:

I already ran this code before, and I am confident the error is not due to some other code part failing. I am writing a parser that iterates over every row in a dataframe and parses a string into multiple columns in the output dataframe. I recently added a new couple of lines inside a for loop:

for word in words:
    word = word.replace("-", " ")
    if word in measurement_units:
        measurement = word
        words.remove(word)

The only added thing is the if statement and what comes after it. Now, my code errors with

    words.remove(word)
ValueError: list.remove(x): x not in list

The error is not common, it goes hundreds of thousands of rows before giving this error, so it cannot be something completely trivial - some type of string clearly triggers it. I am a total beginner, so I already asked bing AI and it wants me to write it as:

words = row.split(" ")
new_words = []
for word in words:
    word = word.replace("-", " ")
    if word not in measurement_units:
        new_words.append(word)
    else:
        measurement = word
    words = new_words

Which works, but why do I need to? In my mind, this already should not be happening - we know the word exists, since the "if" statement already implicitly checks if its "", and there is no removal done between the if and the words.remove. What gives?

Not much - there is only so many test you can write in a 2-line snippet, and since I have well over a million rows, printing every row or every word is not feasible.

答案1

得分: 1

Without any sample data to verify this against it looks like you want:

for word in words[:]:
    if word.replace('-', ' ') in measurement_units:
        words.remove(word)
英文:

Without any sample data to verify this against it looks like you want:

for word in words[:]:
    if word.replace('-', ' ') in measurement_units:
        words.remove(word)

huangapple
  • 本文由 发表于 2023年8月5日 09:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定