PKl*_*mpp 35 python dictionary
Sry如果这个问题已经存在,但我现在已经找了很长时间了.
我在python中有一个字典,我想要做的是从列表中获取一些值,但我不知道这是否得到了实现的支持.
myDictionary.get('firstKey') # works fine
myDictionary.get('firstKey','secondKey')
# gives me a KeyError -> OK, get is not defined for multiple keys
myDictionary['firstKey','secondKey'] # doesn't work either
Run Code Online (Sandbox Code Playgroud)
但有什么办法可以实现这个目标吗?在我的例子中,它看起来很简单,但是假设我有一个包含20个条目的字典,我想获得5个密钥.还有其他方法吗?
myDictionary.get('firstKey')
myDictionary.get('secondKey')
myDictionary.get('thirdKey')
myDictionary.get('fourthKey')
myDictionary.get('fifthKey')
Run Code Online (Sandbox Code Playgroud)
Vee*_*rac 45
已经存在这样的功能:
from operator import itemgetter
my_dict = {x: x**2 for x in range(10)}
itemgetter(1, 3, 2, 5)(my_dict)
#>>> (1, 9, 4, 25)
Run Code Online (Sandbox Code Playgroud)
itemgetter
如果传递了多个参数,将返回一个元组.要将列表传递给itemgetter
,请使用
itemgetter(*wanted_keys)(my_dict)
Run Code Online (Sandbox Code Playgroud)
Com*_*low 33
使用for
循环:
keys = ['firstKey', 'secondKey', 'thirdKey']
for key in keys:
myDictionary.get(key)
Run Code Online (Sandbox Code Playgroud)
或列表理解:
[myDictionary.get(key) for key in keys]
Run Code Online (Sandbox Code Playgroud)
小智 10
因为我在这里没有看到类似的答案 - 值得指出的是,通过使用(列表/生成器)理解,您可以解压这些多个值并将它们分配给单行代码中的多个变量:
first_val, second_val = (myDict.get(key) for key in [first_key, second_key])
Run Code Online (Sandbox Code Playgroud)
您可以At
从 pydash使用:
from pydash import at
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_list = at(my_dict, 'a', 'b')
my_list == [1, 2]
Run Code Online (Sandbox Code Playgroud)
我认为列表理解是最干净的方法之一,不需要任何额外的导入:
>>> d={"foo": 1, "bar": 2, "baz": 3}
>>> a = [d.get(k) for k in ["foo", "bar", "baz"]]
>>> a
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
或者,如果您希望将值作为单独的变量,则使用多重赋值:
>>> a,b,c = [d.get(k) for k in ["foo", "bar", "baz"]]
>>> a,b,c
(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
%timeit
上面列出的所有答案的回应。如果错过了一些解决方案,我深表歉意,并且我用我的判断来组合类似的答案。itemgetter
对我来说似乎是赢家。pydash
报告的时间要少得多,但我不知道为什么它运行的循环更少,也不知道我是否可以称其为最快。你的想法?
from operator import itemgetter\n\nmy_dict = {x: x**2 for x in range(10)}\nreq_keys = [1, 3, 2, 5]\n%timeit itemgetter(1, 3, 2, 5)(my_dict)\n257 ns \xc2\xb1 4.61 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n%timeit [my_dict.get(key) for key in req_keys]\n604 ns \xc2\xb1 6.94 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n\n%timeit list( map(my_dict.get, req_keys) )\n529 ns \xc2\xb1 34.2 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n\n!pip install pydash\nfrom pydash import at\n\n%timeit at(my_dict, 1, 3, 2, 5)\n22.2 \xc2\xb5s \xc2\xb1 572 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n\n\n%timeit (my_dict.get(key) for key in req_keys)\n308 ns \xc2\xb1 6.53 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\ns = pd.Series(my_dict)\n\n%timeit s[req_keys]\n334 \xc2\xb5s \xc2\xb1 58.1 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n
没有人提到过该map
函数,它允许函数在列表上逐个元素地操作:
mydictionary = {'a': 'apple', 'b': 'bear', 'c': 'castle'}
keys = ['b', 'c']
values = map(mydictionary.get, keys)
# values = ['bear', 'castle']
Run Code Online (Sandbox Code Playgroud)
小智 5
如果你已经pandas
安装了,可以把它变成一个系列,以按键为索引。所以像
import pandas as pd
s = pd.Series(my_dict)
s[['key1', 'key3', 'key2']]
Run Code Online (Sandbox Code Playgroud)