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

how to split the string wtih brackets?

I’d like to splitt the below string into something like this
'180-240','0-100','100-200','0-110','200-240','0-120'

a='(180-240,0-100),(100-200,0-110),(200-240,0-120)'
a
'(180-240,0-100),(100-200,0-110),(200-240,0-120)'

After the below split, it is still one element of string, I am expecting to get a lsit with 3 tuples of

['(180-240,0-100','100-200,0-110','200-240,0-120)']

However I got below a list with only one string. Not sure where is the problem? Thanks

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


b=a.split('(,)')
b
['(180-240,0-100),(100-200,0-110),(200-240,0-120)']

>Solution :

One of many possible solutions, use re:

import re

a = "(180-240,0-100),(100-200,0-110),(200-240,0-120)"

print(re.findall(r"[^)(,]+", a))

Prints:

['180-240', '0-100', '100-200', '0-110', '200-240', '0-120']
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