I have a string of the form:
s = "1,2;3,4"
I have to obtain:
res = [(1,2),(3,4)]
I could not came up better than:
pairs = [pair.split(",") for pair in s.split(";")]
res = [(int(a), int(b)) for a,b in pairs]
In fact this doesn't work:
>>> res = [(int(a),int(b)) for pair in s.split(";") for a,b in pair.split(",")]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
ValueError: not enough values to unpack (expected 2, got 1)
My questions are:
- Is there a one-liner solution? What's the most pythonic way to achieve this?
- Why my latter code doesn't work?