组列表通过在Python

问题描述:

一定的价值我有日期的列表,列表中的每个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=0price=None说。

+1

字典中python3.x不支持(点)的访问密钥。 – Rahul

+0

我不知道为什么你可能想组,但一个快速的方法来获得价格清单是'价格= [票[“价格”]为元素票[“门票”]'。 –

+1

建议:不要使用内置名称作为变量(列表)。 – Chikiro

我相信你正在寻找itertools.groupby。为了使其工作,您需要首先对price项目进行排序。

import itertools 

list_ = ... 

for element in list_: 
    groups = [list(g) for _, g in itertools.groupby(sorted(element['tickets'], key=lambda x: x['price']))] 

    if groups: 
     for group in groups: 
      print(element['date'], len(group), group[0]['price'], sep=' - ') 
    else: 
     print(element['date'], 0, None, sep=' - ') 

输出:

X - 3 - 100 
X - 1 - 120 
Y - 2 - 100 
Y - 2 - 300 

不要将其命名列表为list,或作为字典dict,或任何其他内建的名字。


现在,设置list_[1]['tickets'] = [] ...

X - 3 - 100 
X - 1 - 120 
Y - 0 - None 
+0

但是我不能够体验'groups'可能是空的,然后不打印任何东西? – Jamgreen

+1

@Jamgreen组总是1或更大。 –

+0

用这种轻微的问题...做一个'list_ [1] [“门票”] = []'并重新运行......它并不完全符合在Q注:*另外,如果没有日期的门票(即,tickets = []),那么我仍然需要用date =?,num_tickets = 0和price = None来表示行。 –

遍历您的数据和积累的门票价格为collections.Counter然后打印出结果,例如:

from collections import Counter 

for item in data: 
    if not item['tickets']: 
     print(item['date'], 0, 'None', sep=' - ') 
     continue 
    for price, count in Counter(el['price'] for el in item['tickets']).items(): 
     print(item['date'], count, price, sep=' - ') 

给你:

X - 1 - 120 
X - 3 - 100 
Y - 2 - 100 
Y - 2 - 300 
+0

如果OP不担心订购,这是一个很好的解决方案。但是,如果需要排序,则需要排序的呼叫。 –

+0

@cᴏʟᴅsᴘᴇᴇᴅ好的...但是,无论订单是通过入场订单,价格的门票数量还是价格订单 - 都可以在几个地方适当地完成......这对一个群体的好处是我们没有必要对列表进行预先排序来对其进行聚合......即使我们确实需要按键顺序对其进行排序,也可能会有一小排需要在分组之后进行排序的行。 –

+0

已经确认,我是因为这个原因补充了这个解决方案:) –

首先:迭代所有价格以创建列表,然后对其进行分类。第二:将已排序的价格列表反馈给计数器。然后将它与字典列表中的日期结合起来。

from collections import Counter 

data = <data with ticket prices here> 

tickets = [{'date': x['date'], 'tickets_by_price': Counter(sorted([y['price'] for y in x['tickets']]))} for x in data] 

结果:

[ 
    { 
     'tickets_by_price': Counter({100: 3, 120: 1}), 
     'date': 'x' 
    }, 
    { 
     'tickets_by_price': Counter({300: 2, 100: 2}), 
     'date': 'y' 
    } 
]