Rewriting Recipe/410687 without map and lambda

S

sofeng

I would like to use the following recipe to transpose a list of lists
with different lengths. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687

Here is an example of what I would like to do:

Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
a = [[1,2,3],[4,5,6,7],[8,9]]
print map(lambda *row: list(row), *a) [[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]

However, in the Python 3000 FAQ (http://www.artima.com/weblogs/
viewpost.jsp?thread=211200), Guido says not to use map with lambda
because a list comprehension is clearer and faster.

How can I rewrite the above recipe using a list comprehension instead?

-sofeng
 
T

Terry Reedy

|I would like to use the following recipe to transpose a list of lists
| with different lengths.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687
|
| Here is an example of what I would like to do:
|
| Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
| [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
| Type "help", "copyright", "credits" or "license" for more information.
| >>> a = [[1,2,3],[4,5,6,7],[8,9]]
| >>> print map(lambda *row: list(row), *a)
| [[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]
| >>>
|
| However, in the Python 3000 FAQ (http://www.artima.com/weblogs/
| viewpost.jsp?thread=211200), Guido says not to use map with lambda
| because a list comprehension is clearer and faster.
|
| How can I rewrite the above recipe using a list comprehension instead?

Here is the sort of thing Guido is talking about:
a = [[1,2,3],[4,5,6,7],[8,9]]
[list(r) for r in zip(*a)]
[[1, 4, 8], [2, 5, 9]]

Except in this case, zip does not pad, while map does, so the list comp
form has to be
[list(r) for r in map(None, *a)]
[[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]

but at this point, it is about as easy to put the lambda in place of None.

But when one maps one sequence or equal-length sequences, Guido's point
applies.

tjr
 
S

sofeng

|I would like to use the following recipe to transpose a list of lists
| with different lengths.http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687
|
| Here is an example of what I would like to do:
|
| Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
| [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
| Type "help", "copyright", "credits" or "license" for more information.
| >>> a = [[1,2,3],[4,5,6,7],[8,9]]
| >>> print map(lambda *row: list(row), *a)
| [[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]
| >>>
|
| However, in the Python 3000 FAQ (http://www.artima.com/weblogs/
| viewpost.jsp?thread=211200), Guido says not to use map with lambda
| because a list comprehension is clearer and faster.
|
| How can I rewrite the above recipe using a list comprehension instead?

Here is the sort of thing Guido is talking about:
a = [[1,2,3],[4,5,6,7],[8,9]]
[list(r) for r in zip(*a)]

[[1, 4, 8], [2, 5, 9]]

Except in this case, zip does not pad, while map does, so the list comp
form has to be
[list(r) for r in map(None, *a)]

[[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]

but at this point, it is about as easy to put the lambda in place of None.

But when one maps one sequence or equal-length sequences, Guido's point
applies.

tjr

Thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top