![](/img/trans.png)
[英]How do I remove list from list of lists if item is non-numeric or greater than a specific value?
[英]how to remove an item from a list if the list is greater than 1?
我有一个列表,我希望列表只有一项,怎么做? 我试过这样做,但它给了我一个错误: ValueError: list.remove(x): x not in list
这是代码:
List = ["dairy"]
List.append("cheese")
if len(List) > 1:
List.remove(1)
Python 的.remove() 方法将您要删除的值作为其参数,因此它正在寻找列表中的数字 1。 要按索引删除,可以使用 del or.pop()。 例如:
l = ['dairy']
l.append('cheese')
if len(l) > 1:
l.pop() # Removes last item in list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.