-3

I have a string like this:

(63, 166) - (576, 366)

I need to extract the values out so that I have:

x1 = 63
y1 = 166
x2 = 576
y2 = 366

I can easily use the split() function and save the results in temporary arrays and then then further split them etc, but that'll be ugly, I was wondering if there's a slick way to do it? I'm using python.

user961627
  • 12,379
  • 42
  • 136
  • 210

1 Answers1

2

If you can guarantee you don't have negative numbers, then:

from ast import literal_eval

s = '(63, 166) - (576, 366)'
(x1, y1), (x2, y2) = literal_eval(s.replace('-', ','))

Otherwise, you can split on - and do something similar, eg:

(x1, y1), (x2, y2) = map(literal_eval, s.split(' - '))
Jon Clements
  • 138,671
  • 33
  • 247
  • 280