Python不会从列表中删除项目 [英] Python wont remove items from list

查看:98
本文介绍了Python不会从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

filtered_list = ['PerezHilton', 'tomCruise', 'q', 'p']
#BIO[user]['follows'] is just a list of strings say ['a', 'b', 'katieh']
#specs is also a string say eg. 'katieh'
for user in filtered_list:
    if specs not in BIO[user]['follows']:
        filtered_list.remove(user)

上面针对某些共振的代码给出了此错误"ValueError:list.remove(x):x不在列表中",但很明显"p"在列表中,因此为什么它没有检测到"p"却找到了"q'??

The above code for some reson gives this error "ValueError: list.remove(x): x not in list" but clearly 'p' is in the list so why is it not detecting 'p' but it is finding 'q'??

我很困惑,但是可以提供任何帮助,谢谢

Im soo stumped but any help is appreciated, thanks

**对不起,我现在已修复它 *

** SORRY i FIXED IT NOW *

推荐答案

在一行中正确完成此操作的列表理解位于帖子的底部.首先是对问题的一些了解.

The list comprehension that does this correctly in one line is at the bottom of the post. Here's some insight into the problem first.

请勿执行以下操作:

for item in list_:
    list_.remove(item)

因为糟糕而令人困惑的事情发生了.

because bad and confusing things happen.

>>> list_ = range(10)
>>> for item in list_:
...     list_.remove(item)
... 
>>> list_
[1, 3, 5, 7, 9]

每次删除项目时,都会更改其余项目的索引,这会弄乱循环.在遍历列表时从列表中删除项目的一种好方法是按索引进行操作,并向后工作,以使删除不会影响其余的迭代.这样会更好,因为如果删除第9个元素,那么第8个元素仍然是第8个元素,而第10个元素将成为第9个元素.如果您已经处理过该元素,那么您不必在意它的索引是什么.

Every time you remove an item, you change the indexes for the rest of the items which messes up the loop. One good way to remove items from a list while you're traversing it is to do it by index and work backwards so that removals don't affect the rest of the iterations. This is better because if you remove the 9'th element, then the 8'th element is still the 8'th element but the 10'th element becomes the 9'th element. If you've already dealt with that element, then you don't care what its index is.

>>> list_ = range(10)
>>> for i in xrange(len(list_) - 1, -1, -1):
...     del list_[i]
... 
>>> list_
[]

或带有while循环:

i = len(list_)
while i:
    i -= 1
    del list_[i]

因此,在您的情况下,代码应类似于

So in your case, the code would look something like

users[:] = [user for user in users if specs in BIO[user]['follows']]

因为这是一项筛选工作,所以最好使用列表推导来完成. [:]的要点是它分配给列表的一部分,而不是破坏对列表的引用.这意味着对列表的所有其他引用都将被更新.它基本上就位,只是在覆盖原始列表之前先制作了副本 .为了完整起见,下面是使用while循环的方法.

because this is a filtering job and those are best done with list comprehensions. The point of the [:] is that it assigns to a slice of the list instead of clobbering the reference to the list. This means that every other reference to the list will be updated. It's essentially in-place, except that a copy is made before overwriting the original list. For the sake of completeness, here's how to do it with a while loop.

i = len(users)
while i:
    i -= 1
    if specs not in BIO[users[i]]['follows']:
        del users[i]

如果您希望就地完成此操作,则可以执行此操作.这里没有列表的副本.

You could do this if you wanted it done in place. No copy of the list is made here.

这篇关于Python不会从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆