Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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 2.7_Python_Python 2.7_Dictionary - Fatal编程技术网

字典中与键相关的和值-Python 2.7

字典中与键相关的和值-Python 2.7,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我的字典Dict排列如下。每个键都与值列表相关联,其中每个值都是一个元组: Dict = { 'key1': [('Red','Large',30),('Red','Medium',40),('Blue','Small',45)], 'key2': [('Red','Large',35)], 'key3': [('Yellow','Large',30),('Red','Medium',30)], } 然后,我想对给定一个新键的整数(每个元组的索引2)求和,在本例中为

我的字典
Dict
排列如下。每个键都与值列表相关联,其中每个值都是一个元组:

Dict = { 
    'key1': [('Red','Large',30),('Red','Medium',40),('Blue','Small',45)],
    'key2': [('Red','Large',35)],
    'key3': [('Yellow','Large',30),('Red','Medium',30)], 
}
然后,我想对给定一个新键的整数(每个元组的索引2)求和,在本例中为Color

生成的新词典应类似于:

{
    'key1': [('Red', 70), ('Blue', 45)],
    'key2': [('Red', 35)],
    'key3': [('Yellow', 30), ('Red', 30)],
}
我将如何做到这一点

我想的是下面这样,但我知道这在几个方面是错误的

sum = 0
new_dict = {}
new_key = raw_input("Enter a new key to search on: ")
for k,v in Dict:
  if v[0] == new_key:
    sum = sum + v[2]
    new_dict[k].append(sum)
  else:
    sum = 0
    new_dict[k] = [sum]

使用听写理解产生新的输出:

{key: [color, sum(t[2] for t in value if t[0] == color)] for key, value in Dict.iteritems()}
其中
color
是搜索的关键

演示:

要按颜色对所有值求和,请使用
计数器()
对值求和:

from collections import defaultdict, Counter

new_dict = {}
for key, values in Dict.iteritems():
    counts = Counter()
    for color, _, count in values:
        counts[color] += count
    new_dict[key] = counts.items()
其中:

>>> new_dict = {}
>>> for key, values in Dict.iteritems():
...     counts = Counter()
...     for color, _, count in values:
...         counts[color] += count
...     new_dict[key] = counts.items()
... 
>>> new_dict
{'key3': [('Red', 30), ('Yellow', 30)], 'key2': [('Red', 35)], 'key1': [('Blue', 45), ('Red', 70)]}

使用听写理解产生新的输出:

{key: [color, sum(t[2] for t in value if t[0] == color)] for key, value in Dict.iteritems()}
其中
color
是搜索的关键

演示:

要按颜色对所有值求和,请使用
计数器()
对值求和:

from collections import defaultdict, Counter

new_dict = {}
for key, values in Dict.iteritems():
    counts = Counter()
    for color, _, count in values:
        counts[color] += count
    new_dict[key] = counts.items()
其中:

>>> new_dict = {}
>>> for key, values in Dict.iteritems():
...     counts = Counter()
...     for color, _, count in values:
...         counts[color] += count
...     new_dict[key] = counts.items()
... 
>>> new_dict
{'key3': [('Red', 30), ('Yellow', 30)], 'key2': [('Red', 35)], 'key1': [('Blue', 45), ('Red', 70)]}

对不起,打字错误。那应该是Dict。我的原始字典。对不起,错别字。那应该是Dict。我的原始字典。谢谢。这真的很有帮助。如果我没有指定颜色,只想对给定键具有相同颜色的所有内容求和,该怎么办?请参阅上面的“编辑”以获得预期结果。@brno792:您对第二个元素的输出没有意义;它应该是
[('Red',35)]
,列表中的一个元组。对不起,错误地漏掉了元组。现在修好了。那么,如何在不指定颜色的情况下执行此操作?谢谢,谢谢。我还有一个小问题。所以我要求和的量实际上是作为字符串存储在我的原始字典中,所以我不能+=它们。如何将该值转换为浮点值或双精度值?在我的代码中,当创建新的或更早的dict时,我应该在哪里这样做?@brno792:只需使用
counts[color]+=float(value)
即可。谢谢。这真的很有帮助。如果我没有指定颜色,只想对给定键具有相同颜色的所有内容求和,该怎么办?请参阅上面的“编辑”以获得预期结果。@brno792:您对第二个元素的输出没有意义;它应该是
[('Red',35)]
,列表中的一个元组。对不起,错误地漏掉了元组。现在修好了。那么,如何在不指定颜色的情况下执行此操作?谢谢,谢谢。我还有一个小问题。所以我要求和的量实际上是作为字符串存储在我的原始字典中,所以我不能+=它们。如何将该值转换为浮点值或双精度值?在我的代码中,当创建新的或更早的dict时,我应该在哪里这样做?@brno792:只需使用
counts[color]+=float(value)