简体   繁体   English

从一个字典中检索一个键值对作为另一个字典

[英]Retrieve a key-value pair from a dict as another dict

I have a dictionary that looks like: 我有一本字典,看起来像:

{u'message': u'Approved', u'reference': u'A71E7A739E24', u'success': True}

I would like to retrieve the key-value pair for reference, ie { 'reference' : 'A71E7A739E24' } . 我想检索键值对以供参考,即{ 'reference' : 'A71E7A739E24' } I'm trying to do this using iteritems which does return k, v pairs, and then I'm adding them to a new dictionary. 我正在尝试使用iteritems来执行此操作,该iteritems会返回k, v对,然后将它们添加到新字典中。 But then, the resulting value is unicode rather than str for some reason and I'm not sure if this is the most straightforward way to do it: 但是然后,由于某种原因,结果值是unicode而不是str ,我不确定这是否是最简单的方法:

ref = {}
for k, v in charge.iteritems():
    if k == 'reference':
        ref['reference'] = v
        print ref

{'reference': u'A71E7A739E24'} {'参考':u'A71E7A739E24'}

Is there a built-in way to do this more easily? 有内置的方法可以更轻松地执行此操作吗? Or, at least, to avoid using iteritems and simply return: 或者至少,为了避免使用iteritems ,只需返回:

{ 'reference' : 'A71E7A739E24' }

使用迭代项的麻烦在于将查找时间增加到O(n),其中n是字典大小,因为您不再使用哈希表

I dont understand the question: 我不明白这个问题:
is this what you are trying to do: 这是您要执行的操作:
ref={'reference',charge["reference"]}

If you only need to get one key-value pair, it's as simple as 如果您只需要获取一个键值对,则只需

ref = { key: d[key] }

If there may be multiple pairs that are selected by some condition, 如果某个条件可能选择了多对,

either use dict from iterable constructor (the 2nd version is better if your condition depends on values, too): 要么使用可迭代构造函数中的dict (如果您的条件也取决于值,则第二个版本更好):

ref = dict(k,d[k] for k in charge if <condition>)
ref = dict(k,v for k,v in charge.iteritems() if <condition>)

or (since 2.7) a dict comprehension (which is syntactic sugar for the above): 或(自2.7起) dict理解 (以上所述为语法糖):

ref = {k,d[k] for k in charge if <condition>}
<same as above>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Append 一个键值对自动到一个字典 - Append a key-value pair to a dict automatically 使用dict作为另一个python dict(键值对)的复合键 - Use a dict as the composite key of another python dict (key-value pair) 在python字典中添加新的键值对 - Add new key-value pair in python dict 如何基于Python中的匹配键将键值对添加到另一个字典列表中的现有字典列表中 - How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python 如果在至少一个列表中不存在键值对,则从两个字典列表之一中查找并删除该字典 - Find and remove the dict from one of two lists of dicts if there is a key-value pair not present in at least one of the lists 如何从键值对列表构建字典? - How do I build a dict from key-value pair list? 如果键已包含在字典中,如何以智能方式将新的键值对添加到字典中? - How do I add a new key-value pair in a smart way to a dict if the key is already contained in the dict? 你什么时候会在 dict.update 方法中使用键值对而不是 dict? - When would you use a key-value pair over a dict for the dict.update method? 从嵌套字典列表中的字典中删除键值对 - Delete key:value pair from dict in list in nested dict 将dict从键/值对扩展到Python中的键/值对 - Extend dict from key/values pair to key/value pair in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM