如何在将连字符作为值之一的同时对字符串数据类型中的键的列表中的值求和

Posted

技术标签:

【中文标题】如何在将连字符作为值之一的同时对字符串数据类型中的键的列表中的值求和【英文标题】:how to sum the values in a list for a key in string datatype while having hyphen as one of value 【发布时间】:2018-03-19 02:30:06 【问题描述】:

我有一本名为 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'])])

它基本上是与特定 url 相关的 url 和带宽 我需要列表中所有值的总和,这些值在特定键的字符串中,同时忽略作为键值之一的连字符

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

将不胜感激任何帮助

【问题讨论】:

您的第一次尝试应该运行没有错误,但请注意您正在使用if isinstance(i, int) 过滤整数。但是,您的列表只有字符串。 【参考方案1】:

给定

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

【讨论】:

我使用了完全不同的方法,我的回答中对此进行了解释,但是您的代码也可以正常工作【参考方案2】:

实际上我将一个键的所有值转换为整数并将连字符替换为 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])...])

【讨论】:

以上是关于如何在将连字符作为值之一的同时对字符串数据类型中的键的列表中的值求和的主要内容,如果未能解决你的问题,请参考以下文章

Oracle过程增加字符串数据类型并将其存储为表中的列之一

1.Redis简介及Windows安装

python数据类型之一字符串(str)

Swift 元组(Tuple)

在忽略几行的同时在DataFrame上使用.loc

读取csv字典变成str了怎么办