My first idea was also a regex, but regex is not always a good solution.
Don't try to put all your code in one line.
We have enough vertical space, but limited horizontal space.
1 2 3 4 5 6 7 |
def parse_line(line):
result, operands = map ( str .strip, line.split( '=' ))
operands = tuple ( map ( str .strip, operands.split( ',' )))
return result, operands
parse_line(zz)
|
The map function takes as first argument a function, which accepts one argument.
The second argument is an iterable. In the case of split, it's a list.
Each element of the iterable is applied to the function str.split (), which takes a str as argument.
Usually the map function is lazy, this means you have to iterate over the map object.
Argument unpacking happens on the left side, so the map object is implicit unpacked.
In the second line you see, that I use tuple, to consume the map object. It could be also list, set or something else.
There is only a assignment without unpacking.
Output:
('hgmma', ('okpiuutt', 'pddkk', 'iihg'))
Then you can use argument unpacking
1 |
result, operands = parse_line(zz)
|