Python:从列表中删除字典 [英] Python: remove dictionary from list

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

问题描述

如果我有一个字典列表,请说:

If I have a list of dictionaries, say:

[{'id': 1, 'name': 'paul'},
{'id': 2, 'name': 'john'}]

,我想删除 id 2(或名称约翰)的字典,这是以编程方式最有效的方式(也就是说,我不知道列表中的条目的索引,因此不能简单地弹出)。

and I would like to remove the dictionary with id of 2 (or name john), what is the most efficient way to go about this programmatically (that is to say, I don't know the index of the entry in the list so it can't simply be popped).

推荐答案

thelist[:] = [d for d in thelist if d.get('id') != 2]

编辑:由于在对代码性能的评论中表达了一些疑问(一些基于误解Python的性能特征,有些假设超出了给定规定列表中只有一个dict,键值为'id'的值为2),我希望在这一点上提供保证。

Edit: as some doubts have been expressed in a comment about the performance of this code (some based on misunderstanding Python's performance characteristics, some on assuming beyond the given specs that there is exactly one dict in the list with a value of 2 for key 'id'), I wish to offer reassurance on this point.

在旧的Linux框中,测量此代码:

On an old Linux box, measuring this code:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(99)]; import random" "thelist=list(lod); random.shuffle(thelist); thelist[:] = [d for d in thelist if d.get('id') != 2]"
10000 loops, best of 3: 82.3 usec per loop

其中random.shuffle约为57微秒(需要确保要删除的元素不是始终位于同一个位置;-)和0.65初始副本的微秒(无论对Python列表的浅拷贝的性能影响如何,最为明显的是午餐; - ),需要避免在循环中更改原始列表(因此循环中的每一条都有删除的东西; - )。

of which about 57 microseconds for the random.shuffle (needed to ensure that the element to remove is not ALWAYS at the same spot;-) and 0.65 microseconds for the initial copy (whoever worries about performance impact of shallow copies of Python lists is most obviously out to lunch;-), needed to avoid altering the original list in the loop (so each leg of the loop does have something to delete;-).

当知道要删除的项目只有一个项目时,可以更快地找到并删除它:

When it is known that there is exactly one item to remove, it's possible to locate and remove it even more expeditiously:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(99)]; import random" "thelist=list(lod); random.shuffle(thelist); where=(i for i,d in enumerate(thelist) if d.get('id')==2).next(); del thelist[where]"
10000 loops, best of 3: 72.8 usec per loop

(如果您使用的是Python 2.6或更高版本,请使用下一个内置而不是 .next - 但是,如果满足删除条件的符号数量不完全为1,则此代码会分解。概括来说,我们有:

(use the next builtin rather than the .next method if you're on Python 2.6 or better, of course) -- but this code breaks down if the number of dicts that satisfy the removal condition is not exactly one. Generalizing this, we have:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*3; import random" "thelist=list(lod); where=[i for i,d in enumerate(thelist) if d.get('id')==2]; where.reverse()" "for i in where: del thelist[i]"
10000 loops, best of 3: 23.7 usec per loop

可以删除洗牌,因为已经有三个等距离的删除,我们知道,listcomp,不变,价格好:

where the shuffling can be removed because there are already three equispaced dicts to remove, as we know. And the listcomp, unchanged, fares well:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*3; import random" "thelist=list(lod); thelist[:] = [d for d in thelist if d.get('id') != 2]"
10000 loops, best of 3: 23.8 usec per loop

完全是颈部和颈部,甚至只有99个元素被删除,具有更长的列表和更多的重复,这更多地保留: / p>

totally neck and neck, with even just 3 elements of 99 to be removed. With longer lists and more repetitions, this holds even more of course:

$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*133; import random" "thelist=list(lod); where=[i for i,d in enumerate(thelist) if d.get('id')==2]; where.reverse()" "for i in where: del thelist[i]"
1000 loops, best of 3: 1.11 msec per loop
$ python -mtimeit -s"lod=[{'id':i, 'name':'nam%s'%i} for i in range(33)]*133; import random" "thelist=list(lod); thelist[:] = [d for d in thelist if d.get('id') != 2]"
1000 loops, best of 3: 998 usec per loop

总而言之,显然不值得部署制作和撤销索引列表的细微之处,而非完全简单明了的列表理解,在一个小小的情况下可能获得100纳秒 - 在一个较小的情况下损失113微秒;-)。避免或批评简单,直接和完美的性能足够的解决方案(如列表推导一般类别一个列表问题)是一个特别令人讨厌的例子,Knuth和Hoare的着名论文过早优化是编程中所有邪恶的根源! - )

All in all, it's obviously not worth deploying the subtlety of making and reversing the list of indices to remove, vs the perfectly simple and obvious list comprehension, to possibly gain 100 nanoseconds in one small case -- and lose 113 microseconds in a larger one;-). Avoiding or criticizing simple, straightforward, and perfectly performance-adequate solutions (like list comprehensions for this general class of "remove some items from a list" problems) is a particularly nasty example of Knuth's and Hoare's well-known thesis that "premature optimization is the root of all evil in programming"!-)

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

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