Python字典获得多个值

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)

  • 与此有关的一个小问题是,“itemgetter”至少需要一个参数,因此如果您想要执行“itemgetter(*some_keys)(some_dict)”之类的操作,如果“some_keys”是一个空列表,它将失败。 (3认同)
  • 你能缩短多少?如果名称太长... g = itemgetter。O (2认同)
  • 是否有一个选项可以返回指定键列表的键:值对的字典,而不仅仅是值? (2认同)
  • @alancalvitti你总是可以做`{key: mydictionary.get(key) for key in keys}` (2认同)
  • itemgetter 不会处理 KeyError (2认同)

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)

  • 如果保证keys中的每个键都在字典中,你可以写“[myDictionary[key] for key in keys]”,但它并没有比这简单得多。 (4认同)
  • 好的,我已经考虑过这个了,但是真的没有实现的方法吗?我的意思是,我猜这并不奇怪 (2认同)
  • 或对于单行解决方案,在['firstKey','secondKey','thirdKey']]`中为x中的x的[[my_dict.get(x)],但它会尽可能紧凑。 (2认同)

小智 10

因为我在这里没有看到类似的答案 - 值得指出的是,通过使用(列表/生成器)理解,您可以解压这些多个值并将它们分配给单行代码中的多个变量:

first_val, second_val = (myDict.get(key) for key in [first_key, second_key])
Run Code Online (Sandbox Code Playgroud)


bus*_*win 9

您可以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)

  • 我建议在示例中为 `dict` var 选择一个更好的名称。否则它会隐藏内置的“dict”类型。 (6认同)

And*_*own 8

我认为列表理解是最干净的方法之一,不需要任何额外的导入:

>>> 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)


Mai*_*and 7

%timeit上面列出的所有答案的回应。如果错过了一些解决方案,我深表歉意,并且我用我的判断来组合类似的答案。itemgetter对我来说似乎是赢家。pydash报告的时间要少得多,但我不知道为什么它运行的循环更少,也不知道我是否可以称其为最快。你的想法?

\n
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


sco*_*ttt 5

没有人提到过该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)

  • 要使其发挥作用,需要进行一项修改,您必须将“map”与列表一起括起来:“values = list(map(mydictionary.get,keys))” (3认同)

小智 5

如果你已经pandas安装了,可以把它变成一个系列,以按键为索引。所以像

import pandas as pd

s = pd.Series(my_dict)

s[['key1', 'key3', 'key2']]
Run Code Online (Sandbox Code Playgroud)