By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Speaking of list-comprehension?

P: n/a
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[i] >= 10:
.... ta[i] -= 10
.... else:
.... ta[i] += 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]


Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Chinook" wrote:
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[i] >= 10:
... ta[i] -= 10
... else:
... ta[i] += 10
... >>> ta [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

Jul 19 '05 #2

P: n/a
Chinook wrote:
>>> ta = [5, 15, 12, 10, 9]
>>> for i in range(len(ta)): ... if ta[i] >= 10:
... ta[i] -= 10
... else:
... ta[i] += 10
... >>> ta

[15, 5, 2, 0, 19]


One possibility:

py> [(tai + 10, tai - 10)[tai >= 10] for tai in ta]
[15, 5, 2, 0, 19]

But see:

http://www.python.org/peps/pep-0308.html

STeVe
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.