![](/img/trans.png)
[英]In Python, why x = list(map(x)) is ok but x = map(x); x = list(x) causes error?
[英]Python - x not in list error
我在列表中有一个列表,并且我试图删除嵌套列表中等于-1的所有值。 尝试运行代码时,出现“ ValueError:list.remove(x):x不在列表中”错误,知道我在做什么错吗?
for x in list:
for i in x:
if i == -1:
list.remove(x)
遍历列表时,请勿更改列表。 您也不应命名变量list
,因为该名称由内置函数使用。 您可以通过简单的列表理解来实现所需的功能:
my_list = [[x for x in v if x != -1] for v in my_list]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.