Update lists within dictionaries

0

After asking for the votes of the pets, how did I update the new information in the voting list?

mascotas={'Puka':[0,0,0],'Morita':[0,0,0],'Martin':[0,0,0],'Napo':[0,0,0]}
redesSociales=['Facebook','Instagram','Twitter']

votos = list(mascotas.keys())
arreglo = np.array(votos)
valores = list(mascotas.values())
arreglo1 = np.array(valores)

for nombre in mascotas:
    for i in range(len(redesSociales)):
        matriz = input("Ingrese votos obtenidos para %s en %s:" % (nombre,redesSociales[i])).isdigit()
    
asked by Luis Eduardo Cueva Ordoñez 03.01.2019 в 04:14
source

1 answer

0

You have an error in reading the data, since isdigit() returns you a Boolean that is worth True if what the user entered is made up of digits, or False if not , but it does not return the whole as perhaps you thought. For that you must do int() on the value returned by input() .

Once you have read the data correctly, entering it in the corresponding list is very simple, since in mascotas[nombre] you have the list corresponding to that pet, and in the [i] element of that list is where you must enter the value read.

Therefore (I have eliminated the dependencies of numpy, because you do not use them at all in the rest of the code and therefore they are not related to what you ask):

mascotas={'Puka':[0,0,0],'Morita':[0,0,0],'Martin':[0,0,0],'Napo':[0,0,0]}
redesSociales=['Facebook','Instagram','Twitter']

for nombre in mascotas:
    for i in range(len(redesSociales)):
        voto = input("Ingrese votos obtenidos para %s en %s:" % (nombre,redesSociales[i]))
        voto = int(voto)
        mascotas[nombre][i] = voto

After executing this code and answering correlative values between 1 and 12, if you print the variable mascotas you will see:

{'Puka': [1, 2, 3], 'Morita': [4, 5, 6], 'Martin': [7, 8, 9], 'Napo': [10, 11, 12]}

Small improvement .

In the inner loop in which you iterate over i , we need this index i to be able to access mascotas[nombre][i] , but we also need the name of the social network redesSociales[i] to use it in the input() message .

The function enumerate(lista) returns you in each iteration the value of the index, and the value of the element. This avoids having to use range(len()) and having to access redesSociales[i] within the loop, being more readable and more pythonic :

for nombre in mascotas:
    for i, red in enumerate(redesSociales):
        votos = input("Ingrese votos obtenidos para %s en %s:" % (nombre,red))
        votos = int(votos)
        mascotas[nombre][i] = votos

Improvement in data structure

Instead of mascotas being a dictionary already prepared to save the votes that you are reading, it would be better if it were simply the list with the names of the pets.

Having to write all those [0,0,0] "by hand" does not seem like a good idea. And if you increase the number of social networks to four? You would have to change all those initial lists.

It seems better solution to build those lists as you read the data. I propose the following modifications:

  • mascotas is purely a list of names
  • votos will be the dictionary whose keys will be the mascots and whose values will be the lists of votes, but that dictionary is built dynamically as the data is read. Initially empty.

This would be the code (where it is no longer necessary to use enumerate() because we do not access elements of the lists by their index):

mascotas = ['Puka', 'Morita', 'Martin','Napo']
redesSociales = ['Facebook','Instagram','Twitter']

votos = {}

for nombre in mascotas:
    # Crear una lista de votos vacía para esta mascota
    votos[nombre] = []
    for red in redesSociales:
        voto = input("Ingrese votos obtenidos para %s en %s:" % (nombre,red))
        # Añadir este voto a la lista de esta mascota
        votos[nombre].append(int(voto))
    
answered by 03.01.2019 в 13:45