找到&在Python中排序的字典中连续计算相同的值?

时间:2014-12-12 16:04:50

标签: python python-2.7 sorting dictionary

我按其中一个键排序字典,即日期/时间。我想将8个连续匹配的键值(“艺术家”和“专辑”)计为1张专辑。这可能吗?

from datetime import datetime, timedelta
from itertools import groupby

def change_date(self):

    entries = self.data_to_dict()
    entries.sort(key=lambda x: (x['datetime']), reverse=False)

    for x in entries:
        album = x['artist'], x['album']
        grouped_album = [(k, sum(1 for i in g)) for k,g in groupby(album)]
        print grouped_album

以上输出如下。我看到它几乎按照我想要的方式工作,但是groupby可以使用2个键并将它们作为1个元素处理吗?或者我应该一起避免群体?

[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('The War on Drugs', 1), ('Slave Ambient (Deluxe Edition)', 1)]
[('Arcade Fire', 1), ('Funeral', 1)]
[('Arcade Fire', 1), ('Reflektor', 1)]
[('Arcade Fire', 1), ('The Suburbs', 1)]
[('Tame Impala', 1), ('Lonerism', 1)]
[('Tame Impala', 1), ('Lonerism', 1)]

原始数据的格式如下:

[{'album': 'Lonerism', 'song': 'Led Zeppelin (Bonus Track)', 'datetime': '2014-12-10 08:03:00', 'artist': 'Tame Impala'}, {'album': 'Lonerism', 'song': 'Feels Like We Only Go Backwards', 'datetime': '2014-12-10 08:00:00', 'artist': 'Tame Impala'}, {'album': 'The Suburbs', 'song': 'Empty Room', 'datetime': '2014-12-10 07:57:00', 'artist': 'Arcade Fire'}]

1 个答案:

答案 0 :(得分:1)

由于最后3行是在循环中运行的,groupby每次只收到一个专辑。

你应该删除内部(最后4行),并执行以下操作:

首先将entries转换为albums

albums = [(x['artist'], x['album']) for x in entries]

然后将所有相册组合在一起:

grouped_albums = [(k, sum(1 for _ in g)) for k,g in groupby(albums)]

BTW - dicts没有排序。你所拥有的不是一个字典,而是一个元组列表。