简体   繁体   English

如果 x 存在,为什么会发生“ValueError: list.remove(x): x not in list”?

[英]Why 'ValueError: list.remove(x): x not in list' is occurring if x is there?

This is a code that opens a table .csv and read line per line to save data in two variables, saved in a list.这是一个代码,它打开一个表格 .csv 并每行读取一行以将数据保存在两个变量中,保存在一个列表中。 Moreover, I want to filter some data of sheet (.csv) because I don't need it, and I'm filtering applying what is below, there is, x.remove(i) and y.remove(i) but they both are in error as i reported in the title.此外,我想过滤一些工作表 (.csv) 的数据,因为我不需要它,我正在过滤应用下面的内容,有 x.remove(i) 和 y.remove(i) 但它们正如我在标题中所报告的那样,两者都存在错误。 What should i do?我该怎么办? What mistake am i doing?我在做什么错误?

g=float(9.8)

x=[]
y=[]

dados = open('dados123.csv').readlines() #trocar para o nome_do_arquivo após testes de diagnóstico



def adiciona_dados():
    for i in range (len(dados)):
        if i !=0:  
            linha = dados[i].split(";")
            x.append(float(linha[0]))
            y.append(float(linha[1])*g)
        else:
            print('erro1')

len(x)
len(y)
adiciona_dados()



for i in y:  
    if y[i] < 30:

        x.remove(i)
        y.remove(i)              
    else:
        print('nada')

len(x)
len(y)

Orignal code:原始代码:

for i in y:  
    if y[i] < 30:

        x.remove(i)
        y.remove(i)              
    else:
        print('nada')

Modified code version1:修改代码版本1:

for i in y:  
    if i < 30:

        x.remove(i)
        y.remove(i)              
    else:
        print('nada')

Otherwise,Python list method remove() searches for the given element in the list and removes the first matching element.否则,Python 列表方法 remove() 在列表中搜索给定元素并删除第一个匹配元素。 But if two adjacent elements are repeated, it will remove first one and the second will be reserved.但是如果两个相邻的元素重复,它将删除第一个,第二个将被保留。

Modified code version2 for repeated num:修改代码 version2 为重复次数:

for i in y:
    if i < 30:
        while x.count(i)>0:
            x.remove(i)
        while y.count(i)>0:
            y.remove(i)
    else:
        print("nada")

Welcome to Stack Overflow.欢迎使用堆栈溢出。 We didn't have an example of your dadoos123.csv file.我们没有您的dadoos123.csv文件示例。 But we can try to understand what your were trying to do.但是我们可以尝试了解您想要做什么。 Here's my solution for your problem:这是我针对您的问题的解决方案:

import csv

g = 9.80

with open('dados123.csv', newline='') as dados_file:
    dados_reader = csv.reader(dados_file, delimiter=';')
    print('erro1')
    all_linha = list(dados_reader)
    x = [float(current_linha[0]) for current_linha in all_linha[1:] if float(current_linha[0]) >= 30]
    y = [float(current_linha[1]) * g for current_linha in all_linha[1:] if float(current_linha[0]) >= 30]

how_many_nada = (len(all_linha) - 1) - len(x)     # Could also been (len(all_linha) - 1) - len(y). No difference
for current_nada in range(how_many_nada):
    print('nada')

Is that what you were trying to do?这就是你想要做的吗?

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

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