Speaking of list-comprehension?

C

Chinook

I'm probably just getting languages mixed up, but I thought in my Python
readings over the last couple months that I had noticed an either/or
expression (as opposed to a bitwise or, or truth test). Being a curious
sort, I tried several variations of how a list comprehension *might* be
constructed and got the results expected relative to the operators, but
not the results I was trying to achieve.

So, is it possible to achieve what the "for loop" (below) does in a
single list comprehension? I don't even see a way to accomplish such in
two list comprehensions with an intermediate result unless an index
pattern was the criterion.

Just wondering,
Lee C

PS I'm not suggesting it be added to the language :~) Beyond the new
classes and decorators (simply a convienence), I'm for KISS even to the
extent of the much abused Case statement.

>>> ta = [5, 15, 12, 10, 9]
>>> for i in range(len(ta)):
.... if ta >= 10:
.... ta -= 10
.... else:
.... ta += 10
....
>>> ta [15, 5, 2, 0, 19]
>>>
>>> [tai - 10 | tai + 10 for tai in ta if tai >= 10] [29, 29]
>>> [tai - 10 | tai + 10 for tai in ta] [29, -1, -4, -2, 29]
>>> [tai - 10 or tai + 10 for tai in ta] [5, -5, -8, -10, 9]
>>>
 
G

George Sakkis

Chinook said:
I'm probably just getting languages mixed up, but I thought in my Python
readings over the last couple months that I had noticed an either/or
expression (as opposed to a bitwise or, or truth test). Being a curious
sort, I tried several variations of how a list comprehension *might* be
constructed and got the results expected relative to the operators, but
not the results I was trying to achieve.

So, is it possible to achieve what the "for loop" (below) does in a
single list comprehension? I don't even see a way to accomplish such in
two list comprehensions with an intermediate result unless an index
pattern was the criterion.

Just wondering,
Lee C

PS I'm not suggesting it be added to the language :~) Beyond the new
classes and decorators (simply a convienence), I'm for KISS even to the
extent of the much abused Case statement.
ta = [5, 15, 12, 10, 9]
for i in range(len(ta)):
... if ta >= 10:
... ta -= 10
... else:
... ta += 10
...[15, 5, 2, 0, 19]



The following works, although its readability is debatable, to say the
least, let alone performance in the general worst case:
ta = [5, 15, 12, 10, 9]
ta = [(x+10,x-10)[x>=10] for x in ta]
ta
[15, 5, 2, 0, 19]

The lack of a ternary if?then:else operator manifests itself in such
examples, but alas, there isn't much hope that even python 3K will have
one...

Would-go-with-ugly-syntax-than-no-syntax-any-day'ly yrs,

George
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top