473,406 Members | 2,769 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,406 software developers and data experts.

Speaking of list-comprehension?

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
2 1385
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David Mertz | last post by:
In the endless Lisp/macro threads, Alex Martelli mentioned something a bit interesting about screen-reading applications. Specifically, he expressed reservations about whether Python would be a...
4
by: jbailo | last post by:
and it sounds GREAT !!! http://www.virginradio.co.uk/thestation/listen/ogg.html OGG OGG OGG ! ! !
7
by: Neil Zanella | last post by:
OK, this time the compiler's got me a little bit puzzled, simply because it is doing something I am not expecting. My understanding, according to the documentation of std::vector<>::resize(), is...
3
by: Farouq | last post by:
dragon naturally speaking professional version 6.1 Hi all i have recently downloaded some examples from the dragon website in vb. I have managed to change some code to vba so that it could be...
9
by: See_Rock_City | last post by:
Hello All, I've decided that this OOP thing is not just a fad. With that in mind, I'm desparately trying to get rid of my function-oriented design paradigm and switch to a more object-centric...
9
by: CJ Taylor | last post by:
So yeah... finally started playing with Whidbey full on this weekend... anyone else notice that Web Developer gave you pretty much every tool you could ever want? I prototyped an entire data...
17
by: Mike | last post by:
I'm trying to create a page: Three sections (left, topright and bottomright), each with a heading and scrolling (overflow) content. The size of these sections should be based upon the size of the...
3
by: jackiemasson | last post by:
Hi, I want to start Dragon Naturally Speaking when the user enters a field on an Access form and stop when they exit the field. I would like to save the audio file to a specific folder and pass...
4
by: David C. Ullrich | last post by:
Mac OS X has text-to-speech built into the interface. So there must be a way to access that from the command line as well - in fact the first thing I tried worked: os.system('say hello') says...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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,...

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.