reduce to be removed?
Fredrik Lundh
fredrik at pythonware.com
Sat Nov 11 21:08:02 EST 2006
Dustan wrote:
>>>> foo =\
> [[[1,2,3],[4,5,6],[7,8,9]],
> [[3,2,1],[6,5,4],[9,8,7]]]
>
> Here, foo appears to be a 3-dimensional list - except it's supposed to
> be 2-dimensional. The inner-list-of-lists is a result of how I'm
> producing the data, and now I want to do a mass-concatenation (or
> extending) of the inner-list-of-lists, and come up with this result:
>
>>>> foo == [[1,2,3,4,5,6,7,8,9],[3,2,1,6,5,4,9,8,7]]
> True
that still doesn't explain your "the expression must be used in a list
comprehension" requirement, though. assuming that the sizes are
varying, and at least sometimes a lot larger than 3x3, I'd probably
write the above as
for index, item in enumerate(foo):
this = []
for i in item:
this.extend(i)
foo[index] = this
which should be pretty efficient, since it avoids unnecessary function
calls, and is amortized linear time instead of O(N**2).
or, if I was in a hurry, and didn't really care if the inner sequences
were lists or tuples:
foo = map(Tkinter._flatten, foo)
</F>
More information about the Python-list
mailing list