I have a long Python string of the form:
string='Black<5,4>, Black<9,4>'
How can I split this string, and any other of arbitrary length which has the same form (i.e. <ArbitraryString1<ArbitraryListOfIntegers1>,<ArbitraryString2<ArbitraryListOfIntegers2>,...) into a list of tuples.
For example, the following would be the desired output from string:
list_of_tuples=[('Black',[5,4]),'Black,[9,4])
Usually I'd use string.split on the commas to produce a list and then regex to separate the word from the <> but since I need to use commas to delimit my indices (the contents of the <>), this doesn't work.
You may use a regex to capture 1+ word chars before a < and capture everything inside <...> into another group, and then split Group 2 contents with , casting the values to int:
import re
s='Black<5,4>, Black<9,4>'
print([(x, map(int, y.split(','))) for x,y in re.findall(r'(\w+)<([^<>]+)>', s)])
# => [('Black', [5, 4]), ('Black', [9, 4])]
See the Python demo
Pattern details:
(\w+) - group 1 (assigned to x): 1 or more word chars< - a literal <([^<>]+) - Group 2 (assigned to y): 1+ chars other than < and >> - a literal >.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With