For list of dicts: merge same key values, sum another diff. values, count each merge iteration +1 Python

If anybody can help me with some task?

I have list of dicts(or tuples), where:

if tuples:

 comment_id, user_id,     comment_date, comment_time, comment_likes
('51799',   '112801710', '2015-12-07',  '00:03:21',   '0'),
('51761',   '112801710', '2015-12-06',  '19:31:46',   '3'),
('51764',   '112801710', '2015-12-06',  '19:54:19',   '0'),
('51741',   '112801710', '2015-12-06',  '14:17:34',   '2'),
('51768',   '52879933',  '2015-12-06',  '20:03:34',   '0'),
('51766',   '52879933',  '2015-12-06',  '21:33:34',   '0'),

or can be converted to dict like:

{'comm_count': 1, 'user_id': '217407103', 'likes': 0},
  • comment_id – is always unique and cannot meet twice in this list,
  • user_id – is not unique for this list, it can be there as much times as comments were left in the set of posts (naturally I wanted to use this as counter)
  • comment_date and comment_time – can be ignored, needed to sel from db,
  • comment_likes – how much likes each comment has.

The task – make one list of tuples or dictionaries where I have just one ‘user_id’ unique, next how much ‘likes’ each item has (sum) and how much times this comment, with same user id was found in list.

To clarify, this is an expected result:

  user_id,      comment_likes,      comments_left
('112801710',   '5',                '4'),
('52879933',    '0',                '2')

Somehow I made some different sets, but they does not work as expected.

Examples of code:

    for row in results:
    user_id = row[1]        # Get user id ['39411753']
    comm_id = row[0]        # Get post id ['51  575']
    comm_likes = row[4]     # Get post likes ['2']
    comm_likes = int(comm_likes)
    all_users_id_comments.append(user_id)
    if user_id not in temp_list:
        comm_count = 1
        temp_list.append(user_id)
        user_ids_dict = {'user_id':user_id,'likes':comm_likes,'comm_count':comm_count}
        result_dicts_list.append(user_ids_dict)
        if user_id in temp_list:
            for item in result_dicts_list:
                if item['user_id'] == user_id:
                    item['comm_count'] += 1
                    item['likes'] += comm_likes

This way can make list where user_id meet only once and make dict with same user_ids but also with values. Then it checks list with all ids and if this id meet second time – update key values. But result is not correct, I’ve lost comething important.

Another good way to sort:

merged = {}
for dict in user_comments_list_dicts:
for key,value in dict.items():
    if key not in merged:
        merged [key] = []
    merged [key].append(value)
print(merged)

It make one set based on user_id with list of dicts for each comment, which user left:

'144964044': [
          {'comm_id': '51640', 'likes': '0'},
          {'comm_id': '51607', 'likes': '0'},
          {'comm_id': '51613', 'likes': '0'},
          {'comm_id': '51591', 'likes': '1'},
          {'comm_id': '51592', 'likes': '0'},
          {'comm_id': '51317', 'likes': '0'},
          {'comm_id': '51319', 'likes': '0'},
          {'comm_id': '51323', 'likes': '0'}
          ],

But I can’t call value “for 144964044” – it shows me just ‘144964044’ but not that list. Also confuses me.

http://stackoverflow.com/questions/34140400/for-list-of-dicts-merge-same-key-values-sum-another-diff-values-count-each-m

 

About trianglesis

Александр Брюндтзвельт - гений, филантроп, 100 гривен в кармане. Этот блог - "сток" моих мыслей и заметок. Достаточно одного взгляда на него, чтобы понять, что такой же бардак творится у меня в голове. Если вам этот бардак интересен - милости прошу.
Bookmark the permalink.