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 4 1107
I wasn't aware that python supported "if then else".
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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*...
|
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...
|
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...
|
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...
|
by: a |
last post by:
can someone tell me how to use them
thanks
|
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...
|
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
|
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...
|
by: beginner |
last post by:
Hi All,
If I have a list comprehension:
ab=
c = "ABC"
print c
|
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:
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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: 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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |