[英]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.