Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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求和dict键和求和dict值_Python_Dictionary - Fatal编程技术网

python求和dict键和求和dict值

python求和dict键和求和dict值,python,dictionary,Python,Dictionary,首先,这个问题是相关的,但不能解决我的问题 我有一本这样的字典 { ... "httpXYZ_ACTION1": [10, 0], "http123_ITEM1": [0.055, 0.0875], "http456_ACTION1": [0.01824, 0.066667], "httpABC_ITEM2": [1214.666667, 1244.195833], "http999_ACTION2": [null, 213], ... } 我想要的结果是这样的口述 { ... "_ACTI

首先,这个问题是相关的,但不能解决我的问题

我有一本这样的字典

{
...
"httpXYZ_ACTION1": [10, 0],
"http123_ITEM1": [0.055, 0.0875],
"http456_ACTION1": [0.01824, 0.066667],
"httpABC_ITEM2": [1214.666667, 1244.195833],
"http999_ACTION2": [null, 213],
...
}
我想要的结果是这样的口述

{
...
"_ACTION1": [summed up values for all _ACTION1 on any http]
"_ITEM1": [summed up values for all _ITEM1 on any http]
...
}
等等:-)

我试过了

sum(filter(None, chain(*[value for key, value in DICT if key.endswith(('_ACTION1', '_ACTION2', '_ITEM1'))])))

显然,只是将所有内容汇总为一个数字,不知道
null
来自何处,但您可以使用str.find提取部分子字符串,并使用
defaultdict
处理重复键:

from collections import defaultdict

input = {
  "httpXYZ_ACTION1": [10, 0],
  "http123_ITEM1": [0.055, 0.0875],
  "http456_ACTION1": [0.01824, 0.066667],
  "httpABC_ITEM2": [1214.666667, 1244.195833],
  "http999_ACTION2": [None, 213],
}

output = defaultdict(float)
for k,v in input.items():
  key = '_' + k.partition('_')[2]
  output[key] += sum((float(val) for val in v if isinstance(val, (int,float))))

print(output)
from collections import defaultdict
dd = defaultdict(float)

for k, v in d.items():
     dd[k[k.find("_"):]] += sum(v)

print(dd)

defaultdict(<class 'float'>, {'_ITEM1': 0.1425, '_ACTION1': 10.084907, '_ACTION2': 213.0, '_ITEM2': 2458.8625})
或者只保留数字:

 import numbers

 dd[k[k.find("_"):]] + sum(i for i in v if isinstance(i, numbers.Number))
解决方案:

null = 0  # null is invalid in Python unless a variable

data = {
    "httpXYZ_ACTION1": [10, 0],
    "http123_ITEM1": [0.055, 0.0875],
    "http456_ACTION1": [0.075, 0.066667],
    "httpABC_ITEM2": [14.666667, 12.195833],
    "http999_ACTION2": [null, 2],
}

categories = set([c.split('_')[-1] for c in data.keys()])
sums = {k: 0 for k in categories}

for k, v in data.items():
    key = k.split('_')[-1]
    if key in categories:
        sums[key] += 1

print sums
在python 3中运行:

>>> ================================ RESTART ================================
>>> 
{'_ITEM1': 0.1425, '_ITEM2': 2458.8625, '_ACTION2': 213, '_ACTION1': 10.084907}
>>> 
请注意,我将您的
null
值视为
None
,它被视为零,即被忽略。如何求和取决于您。

这是一项工作,因为我们需要注意,例如,在求和[''u ACTION1']中存在一个初始化的浮点,然后才能对其进行+=运算,并以编程方式确保内置的
字典
可能会导致开销

#!/usr/bin/env python3
from collections import defaultdict

DICT = {
    "httpXYZ_ACTION1": [10, 0],
    "http123_ITEM1": [0.055, 0.0875],
    "http456_ACTION1": [0.01824, 0.066667],
    "httpABC_ITEM2": [1214.666667, 1244.195833],
    "http999_ACTION2": [None, 213],
}

sums = defaultdict(lambda: 0.)

# with python2 better use
# for (k, l) in DICT.iteritems():
for (k, l) in DICT.items():
    sums[k[k.find("_"):]] += sum(x for x in l if x is not None)

for pair in sums.items():
    print(pair)
输出:

('_ITEM1', 0.1425)
('_ITEM2', 2458.8625)
('_ACTION2', 213.0)
('_ACTION1', 10.084907)

为什么
分区('''u'[2]
而不是
拆分(''u'[1]
?它们显然是等价的,但后者似乎更容易阅读,也更惯用,因为你只是丢掉了下划线。我认为OP想要保留下划线。@bebop:你错了<代码>分区正是我们正在做的
split()
如果要限制拆分次数,则需要第二个参数,而分区隐式地进行限制。在python中,通常是使用最清晰的工具来完成任务,在本例中,这就是创建分区的用例。这与我提供的答案几乎相同,另外,当您可以简单地使用float时,为什么要使用
lambda:0.
呢?我发现这更具表现力。感谢大家的帮助,这个答案解决了我的问题,其他答案没有解决,因为无法在服务器上导入defaultdict。我的下一个问题是,在这个构造中,我可以将每个键的每个值乘以数字吗60?将倒数第二行更改为“outDict[outKey]=total*60”。这将产生{“uAction2”:12780,“uAction1”:605.09442,“uItem1”:8.54999999999,“uItem2”:147531.75}。再次感谢您的输入!但是我一问就发现了!Python很酷:-)
#!/usr/bin/env python3
from collections import defaultdict

DICT = {
    "httpXYZ_ACTION1": [10, 0],
    "http123_ITEM1": [0.055, 0.0875],
    "http456_ACTION1": [0.01824, 0.066667],
    "httpABC_ITEM2": [1214.666667, 1244.195833],
    "http999_ACTION2": [None, 213],
}

sums = defaultdict(lambda: 0.)

# with python2 better use
# for (k, l) in DICT.iteritems():
for (k, l) in DICT.items():
    sums[k[k.find("_"):]] += sum(x for x in l if x is not None)

for pair in sums.items():
    print(pair)
('_ITEM1', 0.1425)
('_ITEM2', 2458.8625)
('_ACTION2', 213.0)
('_ACTION1', 10.084907)