如何根据其对应的值过滤字典键 [英] How to filter dictionary keys based on its corresponding values
问题描述
我有:
dictionary = {"foo":12, "bar":2, "jim":4, "bob": 17}
我想迭代这个字典,键,所以我可以使用另一个函数中的值。
I want to iterate over this dictionary, but over the values instead of the keys, so I can use the values in another function.
例如,我想测试哪些字典值大于 6
,然后将其密钥存储在列表中。我的代码如下所示:
For example, I want to test which dictionary values are greater than 6
, and then store their keys in a list. My code looks like this:
list = []
for c in dictionary:
if c > 6:
list.append(dictionary[c])
print list
然后,在完美的世界中,列表
将具有所有值大于 6
的键。
但是,我的 for
循环只是迭代键;我想将其改为值!
and then, in a perfect world, list
would feature all the keys whose value is greater than 6
.
However, my for
loop is only iterating over the keys; I would like to change that to the values!
任何帮助都非常感谢。
谢谢你
Any help is greatly appreciated. thank you
推荐答案
>>> d = {"foo": 12, "bar": 2, "jim": 4, "bob": 17}
>>> [k for k, v in d.items() if v > 6] # Use d.iteritems() on python 2.x
['bob', 'foo']
我想只是更新这个答案,也展示了@glarrain的解决方案,我发现自己今天很想使用。
I'd like to just update this answer to also showcase the solution by @glarrain which I find myself tending to use nowadays.
[k for k in d if d[k] > 6]
这是完全交叉兼容的,不需要从 .iteritems
( .iteritems
避免将列表保存到Python 2中的内存,这在Python 3中已修复为。项目
。
This is completely cross compatible and doesn't require a confusing change from .iteritems
(.iteritems
avoids saving a list to memory on Python 2 which is fixed in Python 3) to .items
.
@Falken教授提到了解决这个问题的解决方案
@Prof.Falken mentioned a solution to this problem
from six import iteritems
这有效地修复了交叉兼容性问题,但需要您下载包 6
which effectively fixes the cross compatibility issues BUT requires you to download the package six
然而,我并不完全同意@glarrain这个解决方案更易于阅读,那就是辩论,也许只是一个个人喜好即使Python应该只有一种方式来做到这一点。在我看来,这取决于情况(例如,你可能有一个长的字典名称,你不想输入两次,或者你想给这些值更可读的名字或其他原因)
However I would not fully agree with @glarrain that this solution is more readable, that is up for debate and maybe just a personal preference even though Python is supposed to have only 1 way to do it. In my opinion it depends on the situation (eg. you may have a long dictionary name you don't want to type twice or you want to give the values a more readable name or some other reason)
有趣的时间:
在Python 2中,第二个解决方案比Python 3更快,原始速度相等。
In Python 2, the 2nd solution is faster, in Python 3 they are almost exactly equal in raw speed.
$ python -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k, v in d.items() if v > 6]'
1000000 loops, best of 3: 0.772 usec per loop
$ python -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k, v in d.iteritems() if v > 6]'
1000000 loops, best of 3: 0.508 usec per loop
$ python -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k in d if d[k] > 6]'
1000000 loops, best of 3: 0.45 usec per loop
$ python3 -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k, v in d.items() if v > 6]'
1000000 loops, best of 3: 1.02 usec per loop
$ python3 -m timeit -s 'd = {"foo": 12, "bar": 2, "jim": 4, "bob": 17};' '[k for k in d if d[k] > 6]'
1000000 loops, best of 3: 1.02 usec per loop
但是这些只有测试小字典,在巨大的字典中我很确定没有字典键查找( d [k]
)会使 .items
更快。
这似乎是这样的情况
However these are only tests for small dictionaries, in huge dictionaries I'm pretty sure that not having a dictionary key lookup (d[k]
) would make .items
much faster.
And this seems to be the case
$ python -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k in d if d[k] > 6]'
1 loops, best of 3: 1.75 sec per loop
$ python -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k, v in d.iteritems() if v > 6]'
1 loops, best of 3: 1.71 sec per loop
$ python3 -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k in d if d[k] > 6]'
1 loops, best of 3: 3.08 sec per loop
$ python3 -m timeit -s 'd = {i: i for i in range(-10000000, 10000000)};' -n 1 '[k for k, v in d.items() if v > 6]'
1 loops, best of 3: 2.47 sec per loop
这篇关于如何根据其对应的值过滤字典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!