str-int error and do not let it convert

0
diccionario={1: ['0.18', '0.26', '0.41', '0.11', '0.05'],  2: ['0.34', '0.07', '0.39', '0.16', '0.03']}

for persona in range (1,numpersonas+1):
    filtrada2= [(f,(pes1, pes2, pes3, pes4, pes5)) for (f,(pes1, pes2, pes3, pes4, pes5)) in diccionario.items() if f==persona]
listas>>[1, ('0.18', '0.26', '0.41', '0.11', '0.05'))]
      >>[(2, ('0.34', '0.07', '0.39', '0.16', '0.03'))]
 for fila in filtrada2:
         crit1= crit01+fila[1][0]
         hoja2['K2']= crit1

but row [1] [0] comes out as str and does not let me convert it to int, it gives me the following error

>>>  crit1= crit01+fila[1][0]
TypeError: unsupported operand type(s) for +: 'int' and 'str'

could someone help me ???

int(fila[1][0])
Traceback (most recent call last):

  File "<ipython-input-105-e424d02841a0>", line 1, in <module>
    int(fila[1][0])

ValueError: invalid literal for int() with base 10: '0.18'
    
asked by Perdida en Python 10.10.2018 в 08:58
source

1 answer

0

Several observations regarding your code and your question:

  • The data you have in the dictionary are string lists. When using quotes as in '0.18' , the data is of type str . Therefore, the data you have in lista[1][0] is a string, because already in origin (within the initial dictionary) was, not because it was converted to a string when you pass it to a list, as you mentioned in a comment.
  • If you do not want the data, already in origin, to be strings but floats, do not put them in quotes in the dictionary.
  • If the input data is strings and you can not avoid it, you can convert it to float when you need to operate with them using float(dato) . What you have tried int(dato) does not work because the string contains the "." character that can not appear in an integer (although it does in a float). You can also convert them to float while you are saving them in the list filtrada2 (at the end of this answer I show how).
  • On the other hand, your loop:

    for persona in range (1,numpersonas+1):
        filtrada2= [(f,(pes1, pes2, pes3, pes4, pes5)) for (f,(pes1, pes2, pes3, pes4, pes5)) in diccionario.items() if f==persona]
    

    apparently what you want to do is convert the dictionary into a list. I suppose that you do it so that within it the data sorted by the key appears, that is, first the list whose key in the dictionary is 1 , then the one using key 2 , etc. I understand that you do this because the dictionaries do not guarantee the order.

    However the way you do it is quite inefficient, and incorrect:

    • You make a loop in which persona is taking the values 1, 2 .. which I understand correspond to the keys of your dictionary.
    • For each one, you pick up the whole dictionary by means of a list comprehension to find which element has a key that matches persona , why? Is not it much easier to do diccionario[persona] to get it, instead of traversing the whole dictionary to stay only with the case where f==persona ?
    • Each iteration of the loop saves the result of the list comprehension in the same variable ( filtrada2 ). Each new iteration therefore causes the result of the previous iteration to be lost. When leaving the loop you will have only the list corresponding to the last person.

    I suspect that you do not want to stay with only the last person, since later you try to iterate by filtrada2 as if you had several rows. If what you want is to accumulate the results of the previous loop in a list, then you can do the following:

    filtrada2 = []
    for persona in range(1, numpersonas+1):
        filtrada2.append((persona, diccionario[persona]))
    

    Result:

    >>> filtrada2
    [(1, ['0.18', '0.26', '0.41', '0.11', '0.05']), (2, ['0.34', '0.07', '0.39', '0.16', '0.03'])]
    

    But if the reason for doing this was that you simply wanted to change the dictionary for a list, so that it is sorted by the number of people, you could change the whole previous loop by a line:

    filtrada2 = sorted(diccionario.items())
    

    since diccionario.items() gives you a list of tuples in which each tuple has two elements, the first the dictionary key (number of person) and the second the value (string list), and then sorted() This list is ordered by the first element of each tuple, which is the number of the person.

    Since later you are going to process that list, you do not want the data to be strings, but you need to be float . As I said at the beginning if you can change the input dictionary to remove the quotes, you would already have it. If not, then you have two options:

  • Convert it to float when you are going to operate with the data (ie float(fila[1][i]) , for example.
  • Convert the data to float while you are creating the list filtrada2 .
  • The second option can also be done in many ways. One of them, perhaps the most readable:

    filtrada2 = []
    for persona in range(1, numpersonas+1):
        filtrada2.append((persona, 
                          [float(dato) for dato in diccionario[persona]])
                        )
    

    result:

    >>> filtrada2
    [(1, [0.18, 0.26, 0.41, 0.11, 0.05]), (2, [0.34, 0.07, 0.39, 0.16, 0.03])]
    
        
    answered by 10.10.2018 / 10:04
    source