搜索特定值的嵌套字典列表[重复]

Posted

技术标签:

【中文标题】搜索特定值的嵌套字典列表[重复]【英文标题】:Search list of nested dicts for specific value [duplicate] 【发布时间】:2020-11-27 00:32:40 【问题描述】:

我有一个嵌套字典列表,并且想找到包含某个值的字典,然后使用该字典查找字典中其他键的值。例如,我想在列表中搜索 MP_REFERENCE '123456',以便保存包含此字符串的 dict 并在其中查找其他值(例如 my_dict["OUTCOME_CODE"]["value"])

【问题讨论】:

重复Python list of dictionaries search 【参考方案1】:

这里有一些代码遍历“LIST”并将带有您要求的规范的dict保存到一个名为correct_dict的变量中。请注意,为简单起见,我已将“LIST”重命名为 lst。

lst = [
            
                "RESPONSE_TYPE_CODE": "value": "RNAGE",
                "OUTCOME_CODE": "value": "ACCPT",
                "MP_REFERENCE": "value": 338451,
                "TRANSACTION_REFERENCE": "value": "213527",
                "TRANSACTION_TYPE_CODE": "value": "APPNT",
                "TRANSACTION_STATUS_CODE": "value": "ACCPT",
            ,
            
                "RESPONSE_TYPE_CODE": "value": "RNAGE",
                "OUTCOME_CODE": "value": "ACCPT",
                "MP_REFERENCE": "value": 519505,
                "TRANSACTION_REFERENCE": "value": "123456",
                "TRANSACTION_TYPE_CODE": "value": "APPNT",
                "TRANSACTION_STATUS_CODE": "value": "ACCPT",
            ,
            
                "RESPONSE_TYPE_CODE": "value": "RNAGE",
                "OUTCOME_CODE": "value": "REJCT",
                "MP_REFERENCE": "value": 123456,
                "TRANSACTION_REFERENCE": "value": "475582",
                "TRANSACTION_TYPE_CODE": "value": "DEAPP",
                "TRANSACTION_STATUS_CODE": "value": "ACCPT",
            ]

correct_dict = 
for dic in lst:
    if dic["MP_REFERENCE"]["value"] == "123456":
        correct_dict = dic

【讨论】:

【参考方案2】:
In [64]: LIST = [
    ...:             
    ...:                 "RESPONSE_TYPE_CODE": "value": "RNAGE",
    ...:                 "OUTCOME_CODE": "value": "ACCPT",
    ...:                 "MP_REFERENCE": "value": 338451,
    ...:                 "TRANSACTION_REFERENCE": "value": "213527",
    ...:                 "TRANSACTION_TYPE_CODE": "value": "APPNT",
    ...:                 "TRANSACTION_STATUS_CODE": "value": "ACCPT",
    ...:             ,
    ...:             
    ...:                 "RESPONSE_TYPE_CODE": "value": "RNAGE",
    ...:                 "OUTCOME_CODE": "value": "ACCPT",
    ...:                 "MP_REFERENCE": "value": 519505,
    ...:                 "TRANSACTION_REFERENCE": "value": "123456",
    ...:                 "TRANSACTION_TYPE_CODE": "value": "APPNT",
    ...:                 "TRANSACTION_STATUS_CODE": "value": "ACCPT",
    ...:             ,
    ...:             
    ...:                 "RESPONSE_TYPE_CODE": "value": "RNAGE",
    ...:                 "OUTCOME_CODE": "value": "REJCT",
    ...:                 "MP_REFERENCE": "value": 123456,
    ...:                 "TRANSACTION_REFERENCE": "value": "475582",
    ...:                 "TRANSACTION_TYPE_CODE": "value": "DEAPP",
    ...:                 "TRANSACTION_STATUS_CODE": "value": "ACCPT",
    ...:             
    ...:  ]

In [63]: [d for d in LIST if d["MP_REFERENCE"]["value"] == 123456]
Out[63]:
['RESPONSE_TYPE_CODE': 'value': 'RNAGE',
  'OUTCOME_CODE': 'value': 'REJCT',
  'MP_REFERENCE': 'value': 123456,
  'TRANSACTION_REFERENCE': 'value': '475582',
  'TRANSACTION_TYPE_CODE': 'value': 'DEAPP',
  'TRANSACTION_STATUS_CODE': 'value': 'ACCPT']

【讨论】:

以上是关于搜索特定值的嵌套字典列表[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Python - 在嵌套字典中查找特定值的父键

C# - 字典 - 如何在字典中获取特定类值的最大值? [复制]

从嵌套列表创建字典 [重复]

在字典中创建嵌套列表,列表中没有重复项

试图在嵌套字典中查找唯一值的总和。 (见例子!)

从字典列表中获取特定字典的值[重复]