[英]For list of dicts: merge same key values, sum another diff. values, count each merge iteration +1
我有字典(或元組)列表,其中:
如果元組:
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'),
或可以轉換為dict,例如:
{'comm_count': 1, 'user_id': '217407103', 'likes': 0},
任務-列出一個元組或字典,其中只有一個“ user_id”是唯一的,其次是每個項目有多少“喜歡”(總和)以及在列表中找到具有相同用戶ID的此評論的次數。
為了澄清,這是預期的結果:
user_id, comment_likes, comments_left
('112801710', '5', '4'),
('52879933', '0', '2')
我以某種方式做了一些不同的設置,但它們並沒有按預期工作。
代碼示例:
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
這種方式可以列出僅user_id滿足一次的列表,並使用相同的user_ids和值進行字典。 然后,它將檢查所有ID的列表,以及該ID是否第二次滿足-更新鍵值。 但是結果不正確,我失去了重要的東西。
排序的另一種好方法:
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)
它基於user_id設置了一組,其中包含每個用戶留下的字典列表:
'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'}
],
但是我不能將值稱為“ 144964044”-它僅顯示“ 144964044”,而不顯示該列表。 也讓我感到困惑。
用python解決這個問題會很棒,但是恕我直言,這種情況也可以在SQL db方面解決,我不知道。 也許我可以更新user_id被發現兩次或多次的每一行,並對喜歡的行求和,並在comments_count中為每行添加+1。
python家伙也給了我一個使用建議:理解,集合或鍵\\值-但我全部使用了它們-仍然沒有結果。
我想成為有意識的新手,所以我遵循了您關於MySQL查詢的建議,並找到了這種方式:
"""SELECT SUM(comment_likes) AS value_sum, comment_user_id, COUNT(*)
FROM pub_comments_weekly
GROUP BY comment_user_id"""
這將顯示如下內容:
((7.0, '-80849532', 3),
(0.0, '100072457', 1),
(4.0, '10224064', 7),
(6.0, '10872377', 27),
(1.0, '111612257', 5),
(10.0, '112801710', 10),
(0.0, '112983834', 2),
(3.0, '11374187', 2),
(0.0, '11558683', 1),
(0.0, '118422944', 1),
(0.0, '119641064', 20),
(1.0, '119991466', 7),
(1.0, '121321268', 1),
(0.0, '12542463', 3))...
其中:(喜歡,user_id,評論)
感謝您的幫助!
計數和求和是在數據庫中使用計數,求和函數和分組依據最有效的方法。
出於某種原因,有必要在python中進行操作,使用字典將是我選擇的元組。 我也建議對結果數據結構使用字典詞典,因為這將使訪問更加容易。
list = [ {'comment_id':'51799', 'user_id':'112801710', 'comment_date':'2015-12-07', 'comment_time': '00:03:21', 'comment_likes':'0'},
{'comment_id':'51761', 'user_id':'112801710', 'comment_date':'2015-12-06', 'comment_time':'19:31:46', 'comment_likes':'3'},
{'comment_id':'51764', 'user_id':'112801710', 'comment_date':'2015-12-06', 'comment_time':'19:54:19', 'comment_likes':'0'},
{'comment_id':'51741', 'user_id':'112801710', 'comment_date':'2015-12-06', 'comment_time':'14:17:34', 'comment_likes':'2'},
{'comment_id':'51768', 'user_id':'52879933', 'comment_date':'2015-12-06', 'comment_time':'20:03:34', 'comment_likes':'0'},
{'comment_id':'51766', 'user_id':'52879933', 'comment_date':'2015-12-06', 'comment_time':'21:33:34', 'comment_likes':'0'}]
def combine(list):
result = {}
for item in list:
resItem = result.get(item['user_id'], None)
if not resItem:
resItem = {'comment_likes': int(item['comment_likes']), 'comments_left': 1}
else:
resItem['comment_likes'] += int(item['comment_likes'])
resItem['comments_left'] +=1
result[item['user_id']] = resItem
print result
combine(list)
結果:
{'112801710': {'comment_likes': 5, 'comments_left': 4}, '52879933': {'comment_likes': 0, 'comments_left': 2}}
希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.