【问题标题】:Checking dictionary entry to another dictionary, adding if certain key:value pair in entry检查字典条目到另一个字典,如果某个键:在条目中添加值对
【发布时间】:2016-05-17 04:09:00
【问题描述】:

假设我有两个字典:

dict1 = [{file:2015a, instrument:NES, gain:23}, {file:2015b, instrument:NES, gain:26}, {file:2015d, instrument:NES, gain:25}]

dict2=  [{file:2015a, instrument:NES, gain:3333}, {file:2015c, instrument:PS2, gain:26}, {file:2015d, instrument:NES, gain:4545}]

我想做的是从 dict2 中取出所有使用 instrument=PS2 的字典条目并将它们放入 dict1,忽略 dict2 中的所有其他条目。

或者只是创建一个新字典 dict3,其中包含 dict1 中具有 instrument:NES 的所有条目以及 dict2 中具有 instrument:PS2 的所有条目。

最简单的方法是什么?

【问题讨论】:

  • 它们是列表,而不是字典..

标签: python dictionary


【解决方案1】:

它们是列表中的字典。

>>> dict1=[{'file':'2015a', 'instrument':'NES', 'gain':23}, {'file':'2015b', 'instrument':'NES', 'gain':26}, {'file':'2015d', 'instrument':'NES', 'gain':25}]
>>> dict2=[{'file':'2015a', 'instrument':'NES', 'gain':3333}, {'file':'2015c', 'instrument':'PS2', 'gain':26}, {'file':'2015d', 'instrument':'NES', 'gain':4545}]

您可以在每个列表上使用 filter() 并将它们连接起来以获得最终输出,如下所示:

>>> list(filter(lambda x:x['instrument']=='NES', dict1))+list(filter(lambda x:x['instrument']=='PS2', dict2))
[{'instrument': 'NES', 'gain': 23, 'file': '2015a'}, {'instrument': 'NES', 'gain': 26, 'file': '2015b'}, {'instrument': 'NES', 'gain': 25, 'file': '2015d'}, {'instrument': 'PS2', 'gain': 26, 'file': '2015c'}]
>>> 

以下是各个输出:

>>> list(filter(lambda x:x['instrument']=='NES', dict1))
[{'instrument': 'NES', 'gain': 23, 'file': '2015a'}, {'instrument': 'NES', 'gain': 26, 'file': '2015b'}, {'instrument': 'NES', 'gain': 25, 'file': '2015d'}]
>>> list(filter(lambda x:x['instrument']=='PS2', dict2))
[{'instrument': 'PS2', 'gain': 26, 'file': '2015c'}]

【讨论】:

    【解决方案2】:

    这些是列表,以字典为元素。

    假设,如果键 instrument 的值是 PS2,您希望将字典从 dict2 附加到 dict1

    for d in dict2:
        if d.get('instrument') == 'PS2':
            dict1.append(d)
    

    另一方面,dict[1-2] 不是列表的好名字。

    示例:

    In [153]: dict1 = [{'file': '2015a', 'instrument': 'NES', 'gain': '23'}, {'file': '2015b', 'instrument': 'NES', 'gain': 26}, {'file': '2015d', 'instrument': 'NES', 'gain': 25}]
    
    In [154]: dict2=  [{'file': '2015a', 'instrument': 'NES', 'gain': 3333}, {'file': '2015c', 'instrument': 'PS2', 'gain': 26}, {'file': '2015d', 'instrument': 'NES', 'gain': 4545}]
    
    In [155]: for d in dict2:
       .....:     if d.get('instrument') == 'PS2':
       .....:         dict1.append(d)
       .....:         
    
    In [156]: dict1
    Out[156]: 
    [{'file': '2015a', 'gain': '23', 'instrument': 'NES'},
     {'file': '2015b', 'gain': 26, 'instrument': 'NES'},
     {'file': '2015d', 'gain': 25, 'instrument': 'NES'},
     {'file': '2015c', 'gain': 26, 'instrument': 'PS2'}]
    

    【讨论】:

    • 哈,谢谢,是的,只是示例名称和虚假列表项。没有意识到它们是列表,从现在开始在我的大脑中更容易处理。谢谢!
    【解决方案3】:

    除了以上答案,你还可以这样做:

    tmp_lst = [i for i in dict2 for k,v in i.items() if (k,v)== ('instrument','PS2')]
    print dict1+tmp_lst
    

    将返回

    [{'instrument': 'NES', 'gain': '23', 'file': '2015a'}, {'instrument': 'NES', 'gain': '26', 'file': '2015b'}, {'instrument': 'NES', 'gain': '25', 'file': '2015d'}, {'instrument': 'PS2', 'gain': 26, 'file': '2015c'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-21
      • 2012-09-20
      • 2020-11-20
      • 2020-05-21
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多