Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetch separate coordinates from array list

s = [‘(2,-1)’, ‘(2,2)’]

these are 2 cordinates how can i convert them in this form so that variable x1,x2,y1,y2 contain there value (without using external library)

x1,y1 = 2,-1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x2,y2 = 2,2

I was trying in this way

for i in range(len(s)-1):
    p1 = s[i]
    p2  = s[i+1]
    print p1 ,p2

>Solution :

You could use ast.literal_eval to create a tuple from the string.

from ast import literal_eval
# ...
x1, y1 = literal_eval(s[i])
x2, y2  = literal_eval(s[i+1])
print(x1, y1, x2, y2)

Alternatively, remove the first and last character, split on comma, and convert each part to an int.

def parts(s):
    return map(int, s[1:-1].split(','))
# ...
x1, y1 = parts(s[i])
x2, y2  = parts(s[i+1])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading