How to Convert All Strings in a List of Lists to Integers

Casting from list of lists of strings to list of lists of ints in python

x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
x = [[int(float(j)) for j in i] for i in x]

Convert string list of lists to integer in python

This should help you:

lst = [['1 2 3'], ['4 5 6'],['7 8 9']]
lst = [elem.split(' ') for lstt in lst for elem in lstt]
lst = [[int(num) for num in lstt[:]] for lstt in lst]

Output:

>>> lst
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Converting a list of lists from string to integer without map method

Try using a list comprehension with another list comprehension:

field = [[int(i) for i in x] for x in field]

Or use map with a list comprehension:

field = [list(map(int, x)) for x in field]

And now after both codes, you add:

print(field)

It will output:

[[0, 0, 0, -1, 0, 0, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0, 0, -1, 0, 0], [0, 0, 0, 0, -1, 0, 0, 0, -1, 0], [0, 0, -1, -1, -1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, -1, -1]]

How do I convert all strings in a list of lists to integers?

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]

Convert all strings in a list to int

Given:

xs = ['1', '2', '3']

Use map then list to obtain a list of integers:

list(map(int, xs))

In Python 2, list was unnecessary since map returned a list:

map(int, xs)

Converting elements of list of nested lists from string to integer in python

Recursion would be a good solution to your problem.

def convert_to_int(lists):
return [int(el) if not isinstance(el,list) else convert_to_int(el) for el in lists]
l2 = ['1','4',['7',['8']],['0','1']]  
l3 = ['0',['1','5'],['0','1',['8',['0','2']]]]
convert_to_int(l2)
>>>[1, 4, [7, [8]], [0, 1]]
convert_to_int(l3)
>>>[0, [1, 5], [0, 1, [8, [0, 2]]]]

Converting list of lists of strings into integer in Python

Try:

old = [['29,1,22,14,32,11,11,3'],
['2,3,1,2,1,4,1,1,3,1'],
['5,2,1,1,3,1,2,4,1,1,2,2,2,1,19,2,1,7'],
['2,2,1,5,6,1,2,3,9,2,1,1,2,6,1,1,2,3,1,1,2'],
['29,44,5,8,17,20,26,47,80,29,47,17,23,26,46,69,8,2,5,38,8,5,5']]

new = [[int(x) for x in sublst[0].split(',')] for sublst in old]
# new = [list(map(int, sublst[0].split(','))) for sublst in old] # an alternative

print(new)
# [[29, 1, 22, 14, 32, 11, 11, 3], [2, 3, 1, 2, 1, 4, 1, 1, 3, 1], [5, 2, 1, 1, 3, 1, 2, 4, 1, 1, 2, 2, 2, 1, 19, 2, 1, 7], [2, 2, 1, 5, 6, 1, 2, 3, 9, 2, 1, 1, 2, 6, 1, 1, 2, 3, 1, 1, 2], [29, 44, 5, 8, 17, 20, 26, 47, 80, 29, 47, 17, 23, 26, 46, 69, 8, 2, 5, 38, 8, 5, 5]]

You need to use split to parse each long string into small strings, and then apply int to convert a string into an int.

Converting specific elements in a list of lists from a string to an integer

Your list comprehension is almost correct, you just need to check using isdigit() in the inner loop, and use int(element) or just element depending on the value if isdigit.

new_breakfast_list = [[int(element) for element in row if element.isdigit() else element] for row in list_of_lists]

Alternatively, as you know integer elements occur at index 1, you could use:

for element in list_of_lists:
element[1]=int(element[1])

As you know the indexes you want to change, you should use this method as it saves needlessly looping through every element.

Flatten a list of lists containing single strings to a list of ints

You can simply do this:

allyears = [int(i[0]) for i in allyears]

Because all the elements in your allyears is a list which has ony one element, so I get it by i[0]

The error is because ypu can't convert a list to an int



Related Topics



Leave a reply



Submit