How to add values from a dictionary with arrays by the Python key?

Asked

Viewed 350 times

2

I have the following dictionary:

{
    'tvmv': [121, 250, 48, 45, 54, 120, 115, 138, 60, 30, 274],
    'avic': [358, 60, 40],
    'hotels_resorts': [60, 31, 45, 50, 300, 165, 40, 46],
    'avani': [70, 40],
    'seteais': [164, 115, 78, 54, 45, 17, 180, 220],
    'tvpo': [54],
    'tlis': [54, 45],
    'coimbra': [54],
    'oriente': [54],
    'tvca': [350, 340, 230, 120, 45]
}

What I need is to be able to add the values on the lists individually by the key, so that I get something like:

{ 'tvmv': 1255, 'avic': 458, 'hotel_resorts': 737 , e por ai vai... }

I tried this way:

time = 0
for x, y in times_min.items():
    for min  in y:
        time += min

But this will add up all the values, from the whole dictionary.

4 answers

2


You can use the method dict.items to iterate over each key and dictionary value.

Since the value of each element of the dictionary is a list of numbers (and list are eternal values), you can use the function sum to sum up all values.

From there, you have several options. An alternative is to modify the dictionary itself as you go through it:

for key, val in di.items():
    # Modifica o dicionário:
    di[key] = sum(val)

Or you can create a new dictionary (to avoid modifying the original):

newDi = {}
for key, val in di.items():
    # Adiciona a soma ao `newDi` com chave `key`:
    newDi[key] = sum(val)

And still in that sense of creating a new dictionary, could use a dicionary comprehension:

newDi = { key: sum(val) for key, val in di.items() }

2

Basics of a dictionary to run it:

 'get', 'items', 'keys' 'values'

interacting by keys:

for i in a.keys():
  print(i)

interacting by the values contained in the key:

for i in a.values():
  print(i)

searching for the key:

 a.get("tvmv")

integrating by content:

k = chave
v= valor
for k,v in a.items():
   print(k, " = ", v)

sem a indicação da chave ou valor sera retornado uma tuple.

The sum() function it had summed up content in the list being only whole, as mentioned by the partner in the post.

0

Still have the functional alternatives:

With built-in function map(função, iterável, ...) apply a function that for each tuple in the form of (chave, valor), do the summation of value and return a tuple in format (chave, soma_do_valor) and convert the mapping result into a dictionary:

times_min = {
    'tvmv': [121, 250, 48, 45, 54, 120, 115, 138, 60, 30, 274],
    'avic': [358, 60, 40],
    'hotels_resorts': [60, 31, 45, 50, 300, 165, 40, 46],
    'avani': [70, 40],
    'seteais': [164, 115, 78, 54, 45, 17, 180, 220],
    'tvpo': [54],
    'tlis': [54, 45],
    'coimbra': [54],
    'oriente': [54],
    'tvca': [350, 340, 230, 120, 45]
}

resultado = dict(map(lambda i: (i[0], sum(i[1])), times_min.items()))

print(resultado)
#{'tvmv': 1255, 'avic': 458, 'hotels_resorts': 737, 'avani': 110, 'seteais': 873, 'tvpo': 54, 'tlis': 99, 'coimbra': 54, 'oriente': 54, 'tvca': 1085}

Using the same reasoning with the function starmap() module itertools. The difference between map() and starmap() is that starmap() already unpacks the iterated item:

from itertools import starmap

times_min = {
    'tvmv': [121, 250, 48, 45, 54, 120, 115, 138, 60, 30, 274],
    'avic': [358, 60, 40],
    'hotels_resorts': [60, 31, 45, 50, 300, 165, 40, 46],
    'avani': [70, 40],
    'seteais': [164, 115, 78, 54, 45, 17, 180, 220],
    'tvpo': [54],
    'tlis': [54, 45],
    'coimbra': [54],
    'oriente': [54],
    'tvca': [350, 340, 230, 120, 45]
}

resultado = dict(starmap(lambda k,v: (k, sum(v)), times_min.items()))

print(resultado)
#{'tvmv': 1255, 'avic': 458, 'hotels_resorts': 737, 'avani': 110, 'seteais': 873, 'tvpo': 54, 'tlis': 99, 'coimbra': 54, 'oriente': 54, 'tvca': 1085}

Test the examples in Ideone

0

>>> d = {
...     'tvmv': [121, 250, 48, 45, 54, 120, 115, 138, 60, 30, 274],
...     'avic': [358, 60, 40],
...     'hotels_resorts': [60, 31, 45, 50, 300, 165, 40, 46],
...     'avani': [70, 40],
...     'seteais': [164, 115, 78, 54, 45, 17, 180, 220],
...     'tvpo': [54],
...     'tlis': [54, 45],
...     'coimbra': [54],
...     'oriente': [54],
...     'tvca': [350, 340, 230, 120, 45]
... }
>>> for k, v in d.items():
...     d[k] = sum(v)
...
>>> d
{'tvmv': 1255, 'avic': 458, 'hotels_resorts': 737, 'avani': 110, 'seteais': 873, 'tvpo': 54, 'tlis': 99, 'coimbra': 54, 'oriente': 54, 'tvca': 1085}
>>>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.