How do I convert all strings in a list of lists to integers?
This video is brought to you by Memorabit, an app that sends technical terms, commands and definitions as reminders at unexpected moments, so they stay in memory. Built on cognitive science principles. Start free on web, iOS and Android:
iOS: https://apps.apple.com/app/id6782301691
Android: https://play.google.com/store/apps/detai...
--------------------------------------------------
Read the revolutionary AI books written by Turbina Editore, the first publishing house entirely run by artificial intelligence. This groundbreaking publisher has transformed the literary world, producing the most sought-after AI-generated books. Start reading today at:
https://turbinaeditore.com/en/
--------------------------------------------------
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 How Do I Convert All Strings In A List Of Lists To Integers?
00:32 Accepted Answer Score 654
01:04 Answer 2 Score 29
01:54 Answer 3 Score 9
02:24 Answer 4 Score 21
02:32 Thank you
--
Full question
https://stackoverflow.com/questions/6421...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #integer
#avk47
ACCEPTED ANSWER
Score 654
int() is the Python standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an integer:
>>> int("1") + 1
2
If you know the structure of your list, T1 (that it simply contains lists, only one level), you could do this in Python 3:
T2 = [list(map(int, x)) for x in T1]
In Python 2:
T2 = [map(int, x) for x in T1]
ANSWER 2
Score 29
You can do this with a list comprehension:
T2 = [[int(column) for column in row] for row in T1]
The inner list comprehension ([int(column) for column in row]) builds a list of ints from a sequence of int-able objects, like decimal strings, in row. The outer list comprehension ([... for row in T1])) builds a list of the results of the inner list comprehension applied to each item in T1.
The code snippet will fail if any of the rows contain objects that can't be converted by int. You'll need a smarter function if you want to process rows containing non-decimal strings.
If you know the structure of the rows, you can replace the inner list comprehension with a call to a function of the row. Eg.
T2 = [parse_a_row_of_T1(row) for row in T1]
ANSWER 3
Score 21
Using only list comprehensions:
[[int(y) for y in x] for x in T1]
ANSWER 4
Score 9
I would agree with everyone’s answers so far but the problem is that if you do not have all integers, they will crash.
If you wanted to exclude non-integers then
T1 = (('13', '17', '18', '21', '32'),
('07', '11', '13', '14', '28'),
('01', '05', '06', '08', '15', '16'))
new_list = list(list(int(a) for a in b) for b in T1 if a.isdigit())
This yields only actual digits. The reason I don't use direct list comprehensions is because list comprehension leaks their internal variables.