我有一个名为temp的词典
dict_items([('/history/apollo/', ['6245', '6245', '6245', '6245', '6245', '6245', '6245',
'6245']), ('/shuttle/countdown/', ['3985', '3985', '3985', '3985', '3985', '3985', '3985',
'-', '-', '-', '0', '3985', '4247', '3985', '3985', '3998', '0',
'3985', '3985', '3985', '3985', '4247', '3985', '3985', '398, '3985']), ('/', ['7074', '7074', '7074',
'7074', '7074', '7074', '7074', '7074', '7074', '7074', '70]), ('/images/dual-pad.gif', ['141308', '141308',
'0', '141308', '141308', '141308', '0', '141308', '0', '141308', '141308']),
('/images/NASA-logosmall.gif', ['0', '786', '786', '0', '786', '786', '786',
'786', '786', '786', '786', '786', '786', '786', '786', '0',
'786', '786', '786'])])
它的基本网址和带宽与特定网址相关 我需要列表中所有值的总和,这些值是特定键的字符串,而忽略连字符,这是键的值之一
desired output:
dict_items([('/history/apollo/', ['4996'], ('/', ['70810']), ('/images/dual-
pad.gif', ['113040']), ('/images/NASA-logosmall.gif', ['12576'])])
#Or total value for a key without string
#dict_items([(/history/apollo/, [4996], (/, [70810])(/images/dual-
pad.gif, [113040]), (/images/NASA-logosmall.gif, [12576])])
my code is
sums = {k: sum(i for i in v if isinstance(i, int)) for k, v in temp.items()}
它给了我错误
TypeError:+:'int'和'str'的不支持的操作数类型然后我尝试了
sums = {k: sum(int(i) for i in v) for k, v in [temp.values()]}
然后我试了
print({k:sum(map(int, [v])) for k, v in temp.items()})
没有工作
收到错误:
TypeError: expected string or bytes-like object
非常感谢任何帮助
答案 0 :(得分:0)
<强>鉴于强>
d = dict([
("/history/apollo/", ["6245", "6245", "6245", "6245", "6245", "6245", "6245","6245"]),
("/shuttle/countdown/", ["3985", "3985", "3985", "3985", "3985", "3985", "3985", "-", "-", "-", "0", "3985", "4247", "3985", "3985", "3998", "0", "3985", "3985", "3985", "3985", "4247", "3985", "3985", "398", "3985"]),
("/", ["7074", "7074", "7074", "7074", "7074", "7074", "7074", "7074", "7074", "7074", "70"]),
("/images/dual-pad.gif", ["141308", "141308", "0", "141308", "141308", "141308", "0", "141308", "0", "141308", "141308"]),
("/images/NASA-logosmall.gif", ["0", "786", "786", "0", "786", "786", "786", "786", "786", "786", "786", "786", "786", "786", "786", "0", "786", "786", "786"])
])
<强>代码强>
{k: sum(int(x) for x in v if x.isdigit()) for k, v in d.items()}
输出
{'/': 70810,
'/history/apollo/': 49960,
'/images/NASA-logosmall.gif': 12576,
'/images/dual-pad.gif': 1130464,
'/shuttle/countdown/': 80635}
答案 1 :(得分:0)
实际上我用整数转换了键的所有值,并用0代替连字符,然后用它们各自的键压缩它们
a = 0
g = []
a_dict = {}
with open("log.txt", "r", errors='ignore') as f:
for line in f:
test = line.split()[-1]
if test == '-':
test = test.replace('-', '0')
test = int(test)
else:
test = int(test)
g.append(test)
a = a + 1
print(a)
g
然后,一旦我有这样的字典
dict_items([('/history/apollo/', [6245, 6245, 6245, 6245,
6245, 6245, 6245,6245]), ('/shuttle/countdown/', [3985,
3985,3985, 3985,3985, 3985, 3985, 0(#instead of hyphen), 0(#instead of
hyphen), 0(#instead of hyphen), ...]), ('/', [7074, 7074, 7074, 7074...
('/images/dualpad.gif', [141308, 141308, 0, 141308, 141308,.,.,.]),
('/images/NASA-logosmall.gif', [0, 786, 786, 786, 786, 786, 0,
786,.,.,.,.])])
然后我简单地添加它
d = 0
for key in temp:
temp[key] = [sum(temp[key])]
d = d + 1
print(temp.items())
输出:
dict_items([('/history/apollo/', [49960]), (/images/NASA-logosmall.gif',
[12576]), ('/', [70810]), ('/images/dual-pad.gif', [1130464])...])