+ 5

Type-conversion in lists

How to convert string elements in list to int and float? i have - list = ['1', '87,42', '28', '17,66', '2', '60,52'] i need - list = [1, 87.42, 28 , 17.66 , 2 , 60,52]

17th Feb 2018, 1:07 PM
Sasha Kozachok
Sasha Kozachok - avatar
4 Respuestas
+ 3
Thanks for answer! But i need to keep int because i have this code - l = [1, 2.3, 5, 4.2,2,23.5] for i in l: if type(i) is int: x = l.index(i) val = i * (l[(x)+1]) print (str(val)) results = 2.3 21.0 47.0 from one program's file i get such text data - ['1 line\nlenght = 87,42 m\n\n28 line\nlenght = 17,66 m\n\n2 line\nlenght = 60,52 m\n\n'] And i need to multiple lenghts of lines by count of lines and summ it. I work in an architectural firm and I need this code to simplify the calculations.)
17th Feb 2018, 2:05 PM
Sasha Kozachok
Sasha Kozachok - avatar
+ 3
Here's my example! check with type() function for correct type in list :) https://code.sololearn.com/cpHoYI54X0qe/?ref=app I m not sure it is what you want though :)
17th Feb 2018, 2:44 PM
Sylar
+ 2
look at this code https://code.sololearn.com/cV3bT6N8g2Na/#py first number is amount of lines and it is int and the second is lenght of line and it is float. Finaly i need to multiply lines by lengths, one by one. For this i wrote this function.
17th Feb 2018, 2:20 PM
Sasha Kozachok
Sasha Kozachok - avatar
+ 2
YES!! i did it! look what i meant. https://code.sololearn.com/cV3bT6N8g2Na/#py thank for advices.
17th Feb 2018, 2:37 PM
Sasha Kozachok
Sasha Kozachok - avatar