473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rewriting Recipe/410687 without map and lambda

I would like to use the following recipe to transpose a list of lists
with different lengths. http://aspn.activestate.com/ASPN/Coo.../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
Dec 11 '07 #1
2 1350

"sofeng" <so*******@gmail.comwrote in message
news:ea**********************************@s19g2000 prg.googlegroups.com...
|I would like to use the following recipe to transpose a list of lists
| with different lengths.
http://aspn.activestate.com/ASPN/Coo.../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

Dec 12 '07 #2
On Dec 11, 8:29 pm, "Terry Reedy" <tjre...@udel.eduwrote:
"sofeng" <sofeng...@gmail.comwrote in message

news:ea**********************************@s19g2000 prg.googlegroups.com...
|I would like to use the following recipe to transpose a list of lists
| with different lengths.http://aspn.activestate.com/ASPN/Coo.../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.
Dec 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
25
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until...
3
by: Greg Collins [Microsoft MVP] | last post by:
I have done a bit of research of Url Rewriting, but as yet have been unsuccessful at getting it to work well, and there are issues around what file types are supported and how much code you want to...
7
by: Thomas Nelson | last post by:
Occasionally someone posts to this group complaining about the lack of "repeat ... until" in python. I too have occasionally wished for such a construct, and after some thinking, I came up with...
0
by: Thomas Nelson | last post by:
Occasionally people post complaining about the lack of a "repeat...until" structure in python. I thought about it and came up with this recipe that I like. The only ugly thing is having to use...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
11
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the...
8
by: Michael Tobis | last post by:
I came across the "japh" concept today and decided to do one of my own, obviously, interpreting the 'p' somewhat loosely, http://en.wikipedia.org/wiki/JAPH but I'm not entirely satisfied with...
3
by: Stuart Golodetz | last post by:
Hi, Just wondering why the following code doesn't work: #include <iostream> #include <boost/lambda/lambda.hpp> using namespace boost::lambda; int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.