Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用字典理解计算关键子字符串匹配的字典值小计_Python_Python 3.x_Dictionary_Dictionary Comprehension - Fatal编程技术网

Python 如何使用字典理解计算关键子字符串匹配的字典值小计

Python 如何使用字典理解计算关键子字符串匹配的字典值小计,python,python-3.x,dictionary,dictionary-comprehension,Python,Python 3.x,Dictionary,Dictionary Comprehension,我想把下面的字典逻辑转换成字典理解逻辑,我该怎么做 # Example: You have this dictA as input dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400, 'fish_prowns_fry': 250, 'fish_dry':300} # Q: Print the total of each category, chiken,

我想把下面的字典逻辑转换成字典理解逻辑,我该怎么做

# Example: You have this dictA as input 
dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400, 'fish_prowns_fry': 250, 'fish_dry':300}

# Q: Print the total of each category, chiken, motton and fish items sub total values, using dictionary comprehension?
# Expected Output: {'chicken': 650, 'motton': 750, 'fish': 550}
dictB = dict()

for (k,v) in dictA.items():
    k = k.split('_')[0]
    if dictB.get(k) is None:
        dictB[k] = v
    else:
        dictB[k] = dictB.get(k)+v

print(dictB)
输出:

{'chiken': 650, 'motton': 750, 'fish': 550}

如果您确定订单,可以使用
itertools
中的
groupby

{k: sum(x[1] for x in g) for k, g in groupby(dictA.items(), lambda x: x[0].split('_')[0])}
dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400, 'fish_prowns_fry': 250, 'fish_dry':300}

out = {k: sum(vv for kk, vv in dictA.items() if kk.startswith(k)) for k in set(k.split('_')[0] for k in dictA)}
print(out)
示例

from itertools import groupby

# Example: You have this dictA as input 
dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400, 'fish_prowns_fry': 250, 'fish_dry':300}

dictB = {k: sum(x[1] for x in g) for k, g in groupby(dictA.items(), lambda x: x[0].split('_')[0])}

print(dictB)
# {'chiken': 650, 'motton': 750, 'fish': 550}

另一种解决方案,不带
itertools

{k: sum(x[1] for x in g) for k, g in groupby(dictA.items(), lambda x: x[0].split('_')[0])}
dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400, 'fish_prowns_fry': 250, 'fish_dry':300}

out = {k: sum(vv for kk, vv in dictA.items() if kk.startswith(k)) for k in set(k.split('_')[0] for k in dictA)}
print(out)
印刷品:

{'chiken': 650, 'motton': 750, 'fish': 550}

您可以创建一组唯一键,然后在找到该键时对字典进行迭代,求和值:

dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400, 'fish_prowns_fry': 250, 'fish_dry':300}

key_list = set([item.split('_')[0] for item in dictA.keys()])

dictB = {unique_key: sum(el for key, el in dictA.items() if key.split('_')[0]==unique_key) for unique_key in key_list}
结果:

{'chiken': 650, 'motton': 750, 'fish': 550}

为什么要使用dict comp?这是可行的,但会很难看

我会使用
defaultdict

from collections import defaultdict

dict_b = defaultdict(int)

for k, v in dict_a.items():
    dict_b[k.split('_')[0]] += v