久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

存儲為鍵值對的元組的總和值按鍵列出

Sum values of tuples stored as key value pair in list by key(存儲為鍵值對的元組的總和值按鍵列出)
本文介紹了存儲為鍵值對的元組的總和值按鍵列出的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個類似 [('a',2),('a',3),('b',3),('c',2),('b',4)]

我想對所有相似的鍵求和并得到 [('a',5),('c',2),('b',7)] 以任何順序都可以.

I want to sum all similar keys and get [('a',5),('c',2),('b',7)] in any order is fine.

有沒有比使用字典更好的方法來做到這一點.最好使用列表理解,例如 [i for j in a for ...]

Is there a better way to do this instead of using a dictionary. Preferably using list comprehension something like [i for j in a for ...]

>>> a = [('a',2),('a',3),('b',3),('c',2),('b',4)]
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for i,j in a:
...   d[i] += j 
... 
>>> d
defaultdict(<type 'int'>, {'a': 5, 'c': 2, 'b': 7})
>>> zip(d.keys(),d.values())
[('a', 5), ('c', 2), ('b', 7)]

在 Python-3 中,最后一行需要 list(zip(d.keys(),d.values()))

In Python-3, the last line requires list(zip(d.keys(),d.values()))

推薦答案

這也可以用 itertools.groupby 在列表推導中,盡管您的方法看起來不錯,并且始終使用列表推導并沒有內在的好處.恕我直言,在沒有其他問題的情況下對一兩行額外的代碼進行處理是徒勞的冒險.如果您希望進一步添加到任何計數中,將此結果保留為字典而不是列表也可能有意義 - 字典是更合適的結構.

This is feasible alternatively with itertools.groupby in a list comprehension, although your approach seems fine and there is no inherent benefit to always using list comprehensions. Getting worked up about an extra line or two of code with no further issues is a fruitless venture, IMHO. It may also make sense to maintain this result as a dictionary instead of a list in case you wish to further add to any of the counts - the dictionary is the more suitable structure.

使用 itertools.groupby 方法,您可以根據第一個元組元素對已排序的組求和.

Using the itertools.groupby approach, you are summing the sorted groups based on first tuple elements.

from itertools import groupby
from operator import itemgetter

my_list = [('a',2),('a',3),('b',3),('c',2),('b',4)]
first = itemgetter(0)
sums = [(k, sum(item[1] for item in tups_to_sum))
        for k, tups_to_sum in groupby(sorted(my_list, key=first), key=first)]

輸出:

[('a', 5), ('b', 7), ('c', 2)]

<小時>

這顯然也可以(并且可能更適合)作為字典理解

{(k, sum(item[1] for item in tups_to_sum))
            for k, tups_to_sum in groupby(sorted(my_list, key=first), key=first)}

這篇關于存儲為鍵值對的元組的總和值按鍵列出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品美女www爽爽爽 | 99视频免费播放 | 天天操天天玩 | 蜜桃免费一区二区三区 | 欧美日高清视频 | 97综合在线 | 丁香五月网久久综合 | 欧美亚洲一区二区三区 | 韩国av一区二区 | 国产探花在线精品一区二区 | 国产一区二区三区欧美 | 亚洲视频在线看 | 成人在线观 | 久久久久久国产精品免费免费狐狸 | 久草精品在线 | 久久99精品久久久久久狂牛 | 午夜影院在线观看免费 | 国产婷婷色综合av蜜臀av | 精品伊人久久 | 中文字幕亚洲专区 | 国产欧美一区二区三区久久手机版 | 欧美二区乱c黑人 | 国产成人免费视频网站高清观看视频 | 欧美精品在线看 | 日本精品一区 | 久久精品视频一区二区三区 | 亚洲欧美视频一区 | h视频免费在线观看 | 在线观看第一区 | 欧美精品欧美精品系列 | 欧美精品综合 | 久久久精品一区 | 在线视频亚洲 | 午夜免费电影 | 人人澡视频 | 先锋资源网 | 久久久一区二区 | 成人在线观看黄 | 伊人春色在线观看 | 亚洲精品一二三区 | 国产999精品久久久 精品三级在线观看 |