1
我的字典這樣的清單,獲取特定鍵值的Python
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,而無需重複分段名和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
]
}
[從python詞典列表中獲取值列表](http://stackoverflow.com/q/25040875/2301450)? – vaultah
你認爲,'如果'條件在做什麼? – Daniel
請不要將代碼轉儲到您的問題 - 限制列表的4-5項條目將足以說明您的問題 – scytale