我有一本这样的词典
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'}]
我需要遍历字典列表并获取所有字典的DriverValue,而不重复SegmentName、DriverName和PortfolioId。你知道吗
到目前为止这是我的密码
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)
DriverValue在列表中始终为空。我哪里做错了?你知道吗
样本输出:
{
"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
]
}
可能有一种更优雅的方法,但这将为字典列表中每次出现的“drivervalue”键获取键值对,然后将值存储在列表mm中:
只是随便玩玩,另一种方法是:
其中p是值的列表。希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐