Reputation:
I have a list
a=['10,20,30','30,45,90','40,56,80']
each element of the list is a string. I want to make a list of lists, such that it will look like this:
a=[[10,20,30],[30,45,90],[40,56,80]]
Any ideas? Thanks
Upvotes: 0
Views: 83
Reputation: 32189
You can do it using split
as follows:
b = [map(int, i.split(',')) for i in a]
>>> print b
[[10,20,30],[30,45,90],[40,56,80]]
The split
method splits the string on the specified sub-string, in this case: ','
. A split
without any arguments, such as string.split()
will split the string on a whitespace character and then return a list.
Upvotes: 2
Reputation: 46513
.split
splits the string into strings, so we'll convert each one to int
after splitting.
>>> a = ['10,20,30','30,45,90','40,56,80']
>>> [[int(y) for y in x.split(',')] for x in a]
[[10, 20, 30], [30, 45, 90], [40, 56, 80]]
Upvotes: 2