我有一个大字典对象,有几个键值对(大约 16 个),但我只对其中 3 个感兴趣。对此类字典进行子集化的最佳方法(最短/有效/最优雅)是什么?
我所知道的最好的是:
bigdict = {'a':1,'b':2,....,'z':26}
subdict = {'l':bigdict['l'], 'm':bigdict['m'], 'n':bigdict['n']}
我相信还有比这更优雅的方式。
你可以尝试:
dict((k, bigdict[k]) for k in ('l', 'm', 'n'))
...或者在 Python 2.7 或更高版本中:
{k: bigdict[k] for k in ('l', 'm', 'n')}
我假设您知道密钥将在字典中。如果您不这样做,请参阅 Håvard S 的 answer。
或者,正如 timbo 在评论中指出的那样,如果您想要
bigdict
中缺少的键映射到 None
,您可以这样做:
{k: bigdict.get(k, None) for k in ('l', 'm', 'n')}
如果您使用的是Python 3,并且您只想要新字典中实际存在于原始字典中的键,则可以使用这一事实来查看对象实现一些集合操作:
{k: bigdict[k] for k in bigdict.keys() & {'l', 'm', 'n'}}
至少短一点:
wanted_keys = ['l', 'm', 'n'] # The keys you want
dict((k, bigdict[k]) for k in wanted_keys if k in bigdict)
对所有提到的方法进行一些速度比较:
于2020年7月13日更新(感谢@user3780389): 仅适用于来自 bigdict 的密钥。
IPython 5.5.0 -- An enhanced Interactive Python.
Python 2.7.18 (default, Aug 8 2019, 00:00:00)
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux2
import numpy.random as nprnd
...: keys = nprnd.randint(100000, size=10000)
...: bigdict = dict([(_, nprnd.rand()) for _ in range(100000)])
...:
...: %timeit {key:bigdict[key] for key in keys}
...: %timeit dict((key, bigdict[key]) for key in keys)
...: %timeit dict(map(lambda k: (k, bigdict[k]), keys))
...: %timeit {key:bigdict[key] for key in set(keys) & set(bigdict.keys())}
...: %timeit dict(filter(lambda i:i[0] in keys, bigdict.items()))
...: %timeit {key:value for key, value in bigdict.items() if key in keys}
100 loops, best of 3: 2.36 ms per loop
100 loops, best of 3: 2.87 ms per loop
100 loops, best of 3: 3.65 ms per loop
100 loops, best of 3: 7.14 ms per loop
1 loop, best of 3: 577 ms per loop
1 loop, best of 3: 563 ms per loop
正如预期的那样:字典理解是最好的选择。
interesting_keys = ('l', 'm', 'n')
subdict = {x: bigdict[x] for x in interesting_keys if x in bigdict}
此答案使用与所选答案类似的字典理解,但不会排除缺少的项目。
python 2版本:
{k:v for k, v in bigDict.iteritems() if k in ('l', 'm', 'n')}
python 3版本:
{k:v for k, v in bigDict.items() if k in ('l', 'm', 'n')}
如果您想保留大部分键并删除一些键,则可以使用另一种方法:
{k: bigdict[k] for k in bigdict.keys() if k not in ['l', 'm', 'n']}
也许:
subdict=dict([(x,bigdict[x]) for x in ['l', 'm', 'n']])
Python 3 甚至支持以下内容:
subdict={a:bigdict[a] for a in ['l','m','n']}
请注意,您可以按如下方式检查字典中是否存在:
subdict=dict([(x,bigdict[x]) for x in ['l', 'm', 'n'] if x in bigdict])
分别。对于Python 3
subdict={a:bigdict[a] for a in ['l','m','n'] if a in bigdict}
您还可以使用
map
(无论如何,这是一个非常有用的功能):
sd = dict(map(lambda k: (k, l.get(k, None)), l))
示例:
large_dictionary = {'a1':123, 'a2':45, 'a3':344}
list_of_keys = ['a1', 'a3']
small_dictionary = dict(map(lambda key: (key, large_dictionary.get(key, None)), list_of_keys))
PS:我借用了之前答案中的
.get(key, None)
:)
好吧,这个问题已经困扰了我好几次了,所以谢谢 Jayesh 的提问。
上面的答案似乎是一个很好的解决方案,但是如果您在整个代码中都使用这个解决方案,那么包装功能恕我直言是有意义的。此外,这里有两种可能的用例:一种是您关心所有关键字是否都在原始字典中。还有一个你不知道的地方。平等对待两者就好了。
因此,对于我的二便士价值,我建议编写一个字典的子类,例如
class my_dict(dict):
def subdict(self, keywords, fragile=False):
d = {}
for k in keywords:
try:
d[k] = self[k]
except KeyError:
if fragile:
raise
return d
现在您可以使用
orig_dict.subdict(keywords)
拉出子词典
使用示例:
#
## our keywords are letters of the alphabet
keywords = 'abcdefghijklmnopqrstuvwxyz'
#
## our dictionary maps letters to their index
d = my_dict([(k,i) for i,k in enumerate(keywords)])
print('Original dictionary:\n%r\n\n' % (d,))
#
## constructing a sub-dictionary with good keywords
oddkeywords = keywords[::2]
subd = d.subdict(oddkeywords)
print('Dictionary from odd numbered keys:\n%r\n\n' % (subd,))
#
## constructing a sub-dictionary with mixture of good and bad keywords
somebadkeywords = keywords[1::2] + 'A'
try:
subd2 = d.subdict(somebadkeywords)
print("We shouldn't see this message")
except KeyError:
print("subd2 construction fails:")
print("\toriginal dictionary doesn't contain some keys\n\n")
#
## Trying again with fragile set to false
try:
subd3 = d.subdict(somebadkeywords, fragile=False)
print('Dictionary constructed using some bad keys:\n%r\n\n' % (subd3,))
except KeyError:
print("We shouldn't see this message")
如果运行上述所有代码,您应该看到(类似)以下输出(抱歉格式错误):
原词典:
{'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3, 'g': 6, 'f': 5, “i”:8、“h”:7、“k”:10、“j”:9、“m”:12、“l”:11、“o”:14、 “n”:13,“q”:16,“p”:15,“s”:18,“r”:17,“u”:20, “t”:19、“w”:22、“v”:21、“y”:24、“x”:23、“z”:25}奇数键的字典:
{'a': 0, 'c': 2, 'e': 4, 'g': 6, 'i': 8, 'k': 10, 'm': 12, 'o': 14, ' q':16,'s':18,'u':20,'w':22,'y':24}subd2 构建失败:
原始词典不包含某些键使用一些坏键构造的字典:
{'b':1,'d':3,'f':5,'h':7,'j':9,'l':11,'n':13,'p':15,' r':17,'t':19,'v':21,'x':23,'z':25}
还有一个(我更喜欢 Mark Longair 的答案)
di = {'a':1,'b':2,'c':3}
req = ['a','c','w']
dict([i for i in di.iteritems() if i[0] in di and i[0] in req])
解决方案
from operator import itemgetter
from typing import List, Dict, Union
def subdict(d: Union[Dict, List], columns: List[str]) -> Union[Dict, List[Dict]]:
"""Return a dict or list of dicts with subset of
columns from the d argument.
"""
getter = itemgetter(*columns)
if isinstance(d, list):
result = []
for subset in map(getter, d):
record = dict(zip(columns, subset))
result.append(record)
return result
elif isinstance(d, dict):
return dict(zip(columns, getter(d)))
raise ValueError('Unsupported type for `d`')
使用示例
# pure dict
d = dict(a=1, b=2, c=3)
print(subdict(d, ['a', 'c']))
>>> In [5]: {'a': 1, 'c': 3}
# list of dicts
d = [
dict(a=1, b=2, c=3),
dict(a=2, b=4, c=6),
dict(a=4, b=8, c=12),
]
print(subdict(d, ['a', 'c']))
>>> In [5]: [{'a': 1, 'c': 3}, {'a': 2, 'c': 6}, {'a': 4, 'c': 12}]
py3.8+ 中避免
None
中缺少键的 big_dict
值的另一种方法使用海象:
small_dict = {key: val for key in ('l', 'm', 'n') if (val := big_dict.get(key))}
使用地图(halfdanrump的答案)最适合我,尽管还没有计时......
但是如果你去找一本字典,并且你有一个 big_dict:
所以例如:
big_dict = {'a':1,'b':2,'c':3,................................................}
req = ['a','c','w']
{k:big_dict.get(k,None) for k in req )
# or
{k:big_dict[k] for k in req if k in big_dict)
注意,在相反的情况下,req很大,但my_dict很小,你应该循环遍历my_dict。
一般来说,我们正在做一个交集,问题的复杂度是 O(min(len(dict)),min(len(req)))。 Python 的自己的交集实现考虑了两个集合的大小,所以它看起来是最优的。此外,在 c 语言中并且是核心库的一部分,可能比大多数未优化的 python 语句更快。 因此,我会考虑的解决方案是:
dict = {'a':1,'b':2,'c':3,................................................}
req = ['a','c','w',...................]
{k:dic[k] for k in set(req).intersection(dict.keys())}
它将关键操作移至 python 的 C 代码中,并且适用于所有情况。
如果有人想要字典的前几项
n
而不知道按键:
n = 5 # First Five Items
ks = [*dikt.keys()][:n]
less_dikt = {i: dikt[i] for i in ks}