Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better way to convert list element into a list like this one

i think this should be very easy , but i really don't know how to write it in a better way, if you know please tell me

#there are some points in text files and was read into a list
s = ['3,4','4,5','6,5','7,8']
#break s element to (x,y) form every element should convert to number type
points = [] 
for pStr in s:
    ss = pStr.split(',')
    points.append([int(p) for p in ss])
print(points) #[[3, 4], [4, 5], [6, 5], [7, 8]]

better write it in one line please

like image 911
Max Avatar asked Dec 18 '25 09:12

Max


1 Answers

using a list comprehension:

In [19]: s = ['3,4','4,5','6,5','7,8']

In [21]: [[int(y) for y in x.split(',')] for x in s]
Out[21]: [[3, 4], [4, 5], [6, 5], [7, 8]]
like image 74
Ashwini Chaudhary Avatar answered Dec 20 '25 22:12

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!