[Tutor] reduce with comprehension
János Juhász
janos.juhasz at VELUX.com
Mon Nov 21 08:20:02 CET 2005
Hi,
I can't imagine how this could be made with list comprehension.
>>> import operator
>>> a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9]))
>>> reduce(operator.add, a) # it makes a long list now
([1], [2], [3, 31, 32], [4], [5], [6], [7, 71, 72], [8], [9])
When I make list comprehension, the list hierarchy is allways the same or
deeper than the original hierarchy. But now it should be reduced with one
level.
>>> [item for item in a] # the deepnes is the same
[([1], [2], [3, 31, 32], [4]), ([5], [6], [7, 71, 72]), ([8], [9])]
>>> [(item, item) for item in a] # it is deeper with one level
>>>
Is it possible to substitute reduce with comprehension anyway ?
Yours sincerely,
______________________________
János Juhász
More information about the Tutor
mailing list