Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复';ValueError:list.remove(x):x不在列表中';Python中的错误_Python - Fatal编程技术网

如何修复';ValueError:list.remove(x):x不在列表中';Python中的错误

如何修复';ValueError:list.remove(x):x不在列表中';Python中的错误,python,Python,我试图从我的“分数”列表中删除“数学,76”,但它一直在吐: 第2行,在 删除标记(“数学,76”) ValueError:list。删除(x):x不在列表中 我曾试图改变代码,将数学作为一个单独的实体,但没有成功 Marks=['Amy','Jones','English',72',Math',76',Computer Science',96] 删除标记(“数学,76”) 打印(标记) 我认为输出应该是没有变量的列表,但它只输出完整的列表。list.remove接受一个参数,即您要删除的项

我试图从我的“分数”列表中删除“数学,76”,但它一直在吐:

第2行,在
删除标记(“数学,76”)
ValueError:list。删除(x):x不在列表中
我曾试图改变代码,将数学作为一个单独的实体,但没有成功

Marks=['Amy','Jones','English',72',Math',76',Computer Science',96]
删除标记(“数学,76”)
打印(标记)

我认为输出应该是没有变量的列表,但它只输出完整的列表。

list.remove
接受一个参数,即您要删除的项

从文档中:

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

但是
“Mathematics,76”
不是列表中的元素,因此您会得到错误
ValueError:list。删除(x):x不在列表中
因此,您希望一次删除一个元素

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
Marks.remove("Maths")
Marks.remove(76)
print(Marks)
或者使用for循环

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]

for item in ["Maths", 76]:
    Marks.remove(item)

print(Marks)
输出将是

['Amy', 'Jones', 'English', 72, 'Computer Science', 96]
给定

您将得到此错误,因为确切的字符串“数学,76”不在列表中

字符串“Math”和数字76都在列表中,因此可以分别删除它们:

Marks.remove("Maths")
Marks.remove(76)
您可以将这些条目(假设有第一个和第二个名称)配对成元组,如下所示:

>>> list(zip(Marks[0::2], Marks[1::2]))
[('Amy', 'Jones'), ('English', 72), ('Maths', 76), ('Computer Science', 96)]
Marks = ['Amy', 'Jones', 'English', 72, "Maths, 76", 'Computer Science', 96]
然后您可以删除
('mathematics',76)

或者,您可以从列表中进行词典理解:

>>> {k:v for k,v in zip(Marks[0::2], Marks[1::2])}
{'Amy': 'Jones', 'English': 72, 'Maths': 76, 'Computer Science': 96}

>>> lookup = {k:v for k,v in zip(Marks[0::2], Marks[1::2])}
>>> lookup['Maths']
76
要删除项目,请使用
pop

>>> lookup.pop('Maths')
76
>>> lookup
{'Amy': 'Jones', 'English': 72, 'Computer Science': 96}

您正试图删除列表中不存在的字符串
“Mathematics,76”

你可以:

Marks.remove("Maths")
Marks.remove(76)
您可以更改列表,如:

>>> list(zip(Marks[0::2], Marks[1::2]))
[('Amy', 'Jones'), ('English', 72), ('Maths', 76), ('Computer Science', 96)]
Marks = ['Amy', 'Jones', 'English', 72, "Maths, 76", 'Computer Science', 96]

NB:当我了解你想做什么的时候,你真的应该考虑<强>使用而不是列表< /强>这样的事情。 例如:

marks = {'first_name': 'Amy', 'name': 'Jones', 'English': 72, 'Maths': 76, 'Computer Science': 96}
del marks['Maths'] # to remove the Maths entry

当列表中不存在该元素时,会遇到此错误。我认为最好的方法是在列表中使用if/else

例如:

Marks = ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
Remove_result = [i for i in Marks if i != "Maths, 76"]
Remove_result
out[1].  ['Amy', 'Jones', 'English', 72, 'Maths', 76, 'Computer Science', 96]
删除\u结果
标记
没有区别,因为
标记中不存在
数学,76“


注意:您不能使用
[标记。如果i==“Mathematics,76”]
,请删除标记中i的(i),因为它返回
[]
。非迭代的原因可以满足
if
条件。

您的列表不包含
“Mathematics,76”
。它包含
“数学”
76