# Example usage using list comprehension: # Say you have the following list of lists of strings and want integers x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']] list_of_integers = [[int(float(j)) for j in i] for i in x] print(list_of_integers) --> [[565, 575], [1215, 245], [1740, 245]] # Note, if the strings don't have decimals, you can omit float()
Here is what the above code is Doing:
1. We have a list of lists of strings, x.
2. We want to convert each string to an integer.
3. We use a list comprehension to iterate through each list in x.
4. We use a nested list comprehension to iterate through each string in each list in x.
5. We use int(float(j)) to convert each string to an integer.
6. We store the result in a new list, list_of_integers.
7. We print list_of_integers.