473,461 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what list comprehension can't

Hello,

Can we impose if then else into list comprehension ?
Like we do in lambda-map form:

This code change None into 0
L = [None, 12]
R = map(lambda x: (x==None and [0] or x)[0], L) # [0,12]

pujo

Jul 19 '05 #1
4 1107
I wasn't aware that python supported "if then else".

Jul 19 '05 #2
Hello,

On 24 Jun 2005 11:45:14 -0700, aj****@gmail.com wrote:
Hello,

Can we impose if then else into list comprehension ?
Like we do in lambda-map form:

This code change None into 0
L = [None, 12]
R = map(lambda x: (x==None and [0] or x)[0], L) # [0,12]


Do you mean:
[(x==None and [0] or [x])[0] for x in L]
or [{None:0}.get(x,x) for x in L]
or [x or 0 for x in L]

Well, the third solution doesn't exactly fit the specification but may
be easier to read.
Christophe
Jul 19 '05 #3
Thank you

pujo

Jul 19 '05 #4
On Fri, 24 Jun 2005 21:04:15 +0200, Christophe Delord wrote:
Hello,

On 24 Jun 2005 11:45:14 -0700, aj****@gmail.com wrote:
Hello,

Can we impose if then else into list comprehension ?
Like we do in lambda-map form:

This code change None into 0
L = [None, 12]
R = map(lambda x: (x==None and [0] or x)[0], L) # [0,12]


Do you mean:
[(x==None and [0] or [x])[0] for x in L]
or [{None:0}.get(x,x) for x in L]
or [x or 0 for x in L]

Well, the third solution doesn't exactly fit the specification but may
be easier to read.


Or, more generally,

[0 for x in L if x is None]

which doesn't solve the asked-for question, but is a useful feature.
--
Steven.

Jul 19 '05 #5

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
7
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the...
24
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I...
15
by: Darren Dale | last post by:
Hi, I need to replace the following loop with a list comprehension: res= for i in arange(10000): res=res+i In practice, res is a complex 2D numarray. For this reason, the regular output...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
4
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
11
by: beginner | last post by:
Hi, Does anyone know how to put an assertion in list comprehension? I have the following list comprehension, but I want to use an assertion to check the contents of rec_stdl. I ended up using...
4
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
1
by: Ken Pu | last post by:
Hi all, I observed an interesting yet unpleasant variable scope behaviour with list comprehension in the following code: print print x It outputs:
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.