【发布时间】:2018-05-20 17:23:23
【问题描述】:
我想删除列表中的值
lists = [12,15,15,15,3,15,1,6,4,7,888,56248]
while len(lists) is True:
lists.pop()
print(lists)
但我得到了这个输出:
[12, 15, 15, 15, 3, 15, 1, 6, 4, 7, 888, 56248]
【问题讨论】:
-
len(lists)是一个整数。整数与布尔单例True永远不是同一个对象。也许你的意思只是while lists? -
@Matthias 同意并敲开了。副本正在谈论在改变列表时迭代列表。这只是对条件的误解。
-
您可以使用
while lists: lists.pop()。更好的解决方案是lists = []。如果它必须是相同的列表(具有相同的 id),请使用lists[:] = []。 -
@Matthias
lists.clear() -
很公平。错误的条件是主要问题。而且我的欺骗目标不适用,因为他从列表的末尾弹出,这是安全的。对此感到抱歉。
标签: python python-3.x list