一定的價值我有日期的列表,列表中的每個dict有另一個列表:組列表通過在Python
list = [
{
'date': 'X',
'tickets': [
{ 'price': 100 },
{ 'price': 120 },
{ 'price': 100 },
{ 'price': 100 },
]
},
{
'date': 'Y',
'tickets': [
{ 'price': 300 },
{ 'price': 300 },
{ 'price': 100 },
{ 'price': 100 },
]
}
]
現在我通過日期循環與
print('Date, Number of Tickets')
print('============')
for element in list:
print(element.date + ' - ' + len(element.tickets))
這打印
Date, Number of Tickets
============
X - 4
Y - 4
但我想它打印是
Date, Number of Tickets, Price
============
X - 3 - 100
X - 1 - 120
Y - 2 - 300
Y - 2 - 100
所以我需要它團體票通過每個組的列表和循環。
因此,它可能是這樣的
print('Date, Number of Tickets, Price')
print('============')
for element in list:
groups = group_by(element.tickets, 'price')
for group in groups:
print(element.date + ' - ' + group.num_tickets + ' - ' + group.price)
,但我不知道如何通過組價格的門票。另外,如果沒有票的日期(即門票= []),那麼我還需要一排date=?
,num_tickets=0
和price=None
說。
字典中python3.x不支持(點)的訪問密鑰。 – Rahul
我不知道爲什麼你可能想組,但一個快速的方法來獲得價格清單是'價格= [票[「價格」]爲元素票[「門票」]'。 –
建議:不要使用內置名稱作爲變量(列表)。 – Chikiro