Answers:
您可以尝试:
dict((k, bigdict[k]) for k in ('l', 'm', 'n'))
...或在 Python 3Python 2.7或更高版本(感谢FábioDiniz指出它也适用于2.7):
{k: bigdict[k] for k in ('l', 'm', 'n')}
更新:正如HåvardS指出的那样,我假设您知道密钥将在字典中- 如果您无法做出此假设,请参阅他的答案。另外,正如timbo在评论中指出的那样,如果您希望将缺少的bigdict
键映射到None
,则可以执行以下操作:
{k: bigdict.get(k, None) for k in ('l', 'm', 'n')}
如果您使用的是Python 3,并且只希望原始字典中实际存在的新字典中的键,则可以使用事实来查看对象实现一些set操作:
{k: bigdict[k] for k in bigdict.keys() & {'l', 'm', 'n'}}
{k: bigdict.get(k,None) for k in ('l', 'm', 'n')}
通过将新字典中的键设置为“无”,可以处理源字典中缺少指定键的情况
bigdict.keys() & {'l', 'm', 'n'}
==> bigdict.viewkeys() & {'l', 'm', 'n'}
对于Python2.7
短一点,至少:
wanted_keys = ['l', 'm', 'n'] # The keys you want
dict((k, bigdict[k]) for k in wanted_keys if k in bigdict)
dict((k,bigdict.get(k,defaultVal) for k in wanted_keys)
如果您必须拥有所有键。
所有提到的方法的速度比较:
Python 2.7.11 |Anaconda 2.4.1 (64-bit)| (default, Jan 29 2016, 14:26:21) [MSC v.1500 64 bit (AMD64)] on win32
In[2]: import numpy.random as nprnd
keys = nprnd.randint(1000, size=10000)
bigdict = dict([(_, nprnd.rand()) for _ in range(1000)])
%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 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: 3.09 ms per loop
100 loops, best of 3: 3.72 ms per loop
100 loops, best of 3: 6.63 ms per loop
10 loops, best of 3: 20.3 ms per loop
100 loops, best of 3: 20.6 ms per loop
不出所料:词典理解是最好的选择。
该答案使用与所选答案相似的字典理解,但除了缺失项外,不会。
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')}
也许:
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}
a
是不是在bigdict
好的,这让我有些困扰,所以谢谢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}
subdict(orig_dict, keys, …)
呢?
解
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}]
bigdict
不包含k