Reputation: 15476
I have a tuple
that contains both scalars and lists
(25.69, [25.5, 29.5], [0.00153, 0.0222], 0.0, [0.04109, 0.08877], [0.31393, 0.34876], [0.75, 0.63])
I need to operate on the first tuple
such that the result is:
((25,69, 25.5, 0.00153, 0.0, 0.04109, 0.31393, 0.75), (25.69, 29.5, 0.222, 0.0, 0.08877, 0.34876, 0.63))
The first list represents a combination of parameters that need to be passed to functions. I need to convert it to a tuple
of two tuples
so each can be passed to the function.
I've looked at zip
, map
, and itertools.izip_longest
but each requires the args to already be split.
Upvotes: 0
Views: 453
Reputation: 369064
Using zip
with generator expression, conditional expression:
>>> xs = (25.69, [25.5, 29.5], [0.00153, 0.0222],
.... 0.0, [0.04109, 0.08877], [0.31393, 0.34876], [0.75, 0.63])
>>> zip(*(x if isinstance(x, list) else [x, x] for x in xs)) # list(zip(...)) in Py 3
[(25.69, 25.5, 0.00153, 0.0, 0.04109, 0.31393, 0.75),
(25.69, 29.5, 0.0222, 0.0, 0.08877, 0.34876, 0.63)]
The conditional expression was used to convert scalar values to lists.
x if isinstance(x, list) else [x, x]
Upvotes: 3