簡體   English   中英

如何快速計算從字典列表中分配給列總和的元素數量?

[英]How can I count the number of elements given to a column sum from a list of dictionaries quickly?

呼! 這個頭銜是滿口的。

我有一個字典列表,從中我試圖提取以下內容:

for code in codes:
    for type in types:
        for color in colors:
            count  = sum(1               for d in data if d.get('code') == code and d.get('type') == type and d.get('color') == color)
            amount = sum(d.get('amount') for d in data if d.get('code') == code and d.get('type') == type and d.get('color') == color)
            cost   = sum(d.get('cost')   for d in data if d.get('code') == code and d.get('type') == type and d.get('color') == color)

我知道我可以將最后兩行更改為:

tally = {k: sum(d.get(k) for d in data if d.get('code') == code and d.get('type') == type and d.get('color') == color) for k in ('amount', 'cost')}

但是上面的行中是否也包含計數?

您可以在此處使用條件表達式

tally = {k: sum(d.get(k) if k != 'count' else 1 for d in data if d.get('code') == code and d.get('type') == type and d.get('color') == color) for k in ('count', 'amount', 'cost')}

我不會為每個(代碼,類型,顏色)組合拖曳d,而是將它傳遞一次並存儲結果:

from collections import defaultdict

counts  = defaultdict(int)
amounts = defaultdict(float)
costs   = defaultdict(float)

for d in data:
    code  = d.get('code')
    type  = d.get('type')
    color = d.get('color')
    if code in codes and type in types and color in colors:
        key = (code, type, color)
        counts [key] += 1
        amounts[key] += d.get('amount')
        costs  [key] += d.get('cost')

為了獲得最大速度,代碼,類型和顏色應分別是集合或字典的鍵。

考慮到這里的嵌套循環是三重嵌套,我認為值得索引字典列表:

from collections import defaultdict
data_entries = defaultdict(list)
for d in data:
    data_entries[(d['code'], d['type'], d['color'])].append(d)
for code in codes:
    for type in types:
        for color in colors:
            entries = data_entries[(code, type, color)]
            count = len(entries)
            amount = sum(d.get('amount', 0) for d in entries)
            cost = sum(d.get('cost', 0) for d in entries)

然后,您也可以使用Ashwini Chaudhary的答案中的條件表達式,但是當您只需要對實際匹配的條目求和時,它應該會明顯更快。 tally['count'] = len(entries)應該比dict理解要快,但是確實需要更多的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM