简体   繁体   English

从所有字典中获取特定键的值

[英]Get the value of particular key from all dictionaries Python

I have a list of dictionary like this, 我有这样的字典清单,

a = [{'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.921996823602789',
      'MonthsOnBooks': '1',
      'CohortQuarterYear': 'Q1 2015', 'PortfolioId': '55dc0e48c95a8a1a2ffb836e', 'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.9123400335106416',
      'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.9024207736261439',
      'MonthsOnBooks': '3', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.889915542915732',
      'MonthsOnBooks': '4',
      'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'), 'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '01-01-2015', 'DriverValue': '0.8806580068608421',
      'MonthsOnBooks': '5', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9458702933667549',
      'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9335318149106486',
      'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9225337221412799',
      'MonthsOnBooks': '3', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '02-01-2015', 'DriverValue': '0.9109689291572624',
      'MonthsOnBooks': '4', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '03-01-2015', 'DriverValue': '0.9092991755453589',
      'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '03-01-2015', 'DriverValue': '0.8988101317456371',
      'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '03-01-2015', 'DriverValue': '0.8860809446836048',
      'MonthsOnBooks': '3', 'CohortQuarterYear': 'Q1 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '04-01-2015', 'DriverValue': '0.9471495415096017',
      'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q2 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '04-01-2015', 'DriverValue': '0.9364314248533585',
      'MonthsOnBooks': '2', 'CohortQuarterYear': 'Q2 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'},
     {'SegmentName': 'FICO High', 'CohortMonthYear': '05-01-2015', 'DriverValue': '0.9344317261744861',
      'MonthsOnBooks': '1', 'CohortQuarterYear': 'Q2 2015', 'PortfolioId': ('55dc0e48c95a8a1a2ffb836e'),
      'DriverName': 'Active rate'}]

I need to iterate over the list of dictionaries and get the DriverValue of all dictionary without repeating the SegmentName and DriverName and PortfolioId. 我需要遍历词典列表并获取所有词典的DriverValue,而无需重复SegmentName和DriverName和PortfolioId。

This is my code so far, 到目前为止,这是我的代码,

mm = []
for i in a:
    ss = {'DriverName': i['DriverName'], 'SegmentName': i['SegmentName'], 'PortfolioId': i['PortfolioId'],
          'DriverValue': []}

    if all([i['DriverName'] == 'DriverName' and i['SegmentName'] == 'SegmentName']):
        ss['DriverValue'].append(i['DriverValue'])

    mm.append(ss)

print(mm)

The DriverValue is always empty in the list. DriverValue在列表中始终为空。 Where I am doing wrong? 我在哪里做错了?

Sample Output: 样本输出:

{
    "PortfolioId" : ("55d4247b119a612af00eff4b"),
    "DriverName" : "Active rate",
    "SampleData" : [ 
        30, 
        24, 
        6, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9, 
        9
    ]
}

Theres probably a more elegant approach to this, but this will get you the key value pairs for each occurrence of the "drivervalue" key in the list of dictionaries, and then store the values in your list mm: 可能有一种更优雅的方法,但这将为您提供字典列表中每次出现的“ drivervalue”键的键值对,然后将值存储在列表mm中:

mm = []
for b in a:
    for key,value in b.items():
        if key == "DriverValue":
            mm.append(value)

Just played around and another approach is: 只是玩耍,另一种方法是:

p = [d['DriverValue'] for d in a]

Where p will be a list of the values. 其中p是值的列表。 Hope this helps. 希望这可以帮助。

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

相关问题 如何使用python获取字典列表中的特定键值 - How to get a particular key value in a list of dictionaries using python 如何根据所有词典中都不存在的特定键值从词典列表中获取值? - How to get value from list of dictionaries based on specific key value which does not exist in all dictionaries? 使用 python 从特定键的字典列表中获取值 - Get a value out of a list of dictionaries from a specific key with python 如何从 python 中的字典列表中获取键、值 - How to get key, value from list of dictionaries in python 将字典与特定键组合:Python - Combining dictionaries with a particular key: Python 根据词典列表中的用户输入,打印特定的键值 - Print particular key value based on the user input from list of dictionaries Python:从列表中删除具有相同键值的字典,以便该键的值对于列表中的所有字典都是唯一的 - Python: remove dictionaries from a list which have same value for a key so that the values of that key are unique for all the dictionaries in the list 如何从数据框中的字典列表中存储特定的键值 - How to store a particular key value from a list of dictionaries in a data frame Python - 从字典列表中提取键和值 - Python - extracting key and value from list of dictionaries 从字典列表中获取字典中键的值 - Get value of key in dict from a list of dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM