I have dictionary by the name of 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'])])
its basically url and bandwidth acossiated with the particular url I need sum of all the values in the list which are in string for a particular key while ignoring hyphen which is one of the value for a key
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()}
it gives me error TypeError: unsupported operand type(s) for +: 'int' and 'str' then I tried
sums = {k: sum(int(i) for i in v) for k, v in [temp.values()]}
then I tried
print({k:sum(map(int, [v])) for k, v in temp.items()})
didnt work
getting error :
TypeError: expected string or bytes-like object
will appreciate any help
Given
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"])
])
Code
{k: sum(int(x) for x in v if x.isdigit()) for k, v in d.items()}
Output
{'/': 70810,
'/history/apollo/': 49960,
'/images/NASA-logosmall.gif': 12576,
'/images/dual-pad.gif': 1130464,
'/shuttle/countdown/': 80635}