473,785 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1409
"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
3121
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 good language for visually impaired or blind programmers. The concern, I think, is that pronouncing 'space-space-space-space-space-space-space-space' isn't all that easy to follow if spoken with every line. Even a reduced form like...
4
1583
by: jbailo | last post by:
and it sounds GREAT !!! http://www.virginradio.co.uk/thestation/listen/ogg.html OGG OGG OGG ! ! !
7
1825
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 that when you specify a second argument the number of elements specified in the first argument is each in turn set to the second argument. void Foo::bar(int x) { static std::vector<int> foo; std::cout << "source: " << x << std::endl;...
3
4162
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 used in MS access 2000. But i am stuck on this area, which is: ' Check for a valid speaker TryUsers
9
1316
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 view of the world. Migrating some aspects of my antiquated style has been straight-forward. For example: I used to do this:
9
1085
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 driven web site in 14 hours...yeah, with roles and what not. Oh.. and then I only needed to write 4 lines of code (excluding stored procedures)....
17
2143
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 user's viewable area in their browser. I'm close... really close, but I'm confused over how to mix specific measurements with percentages. When I began, things were quite nice: (http://play.psmonopoly.com/autosize.html). A little clunky in...
3
6223
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 the file name from a field value on the form. I also want the text to be inserted into the field. Does Dragon accept command line parameters on starting? Jackie Masson
4
1385
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 'hello'. Is there something similar in Windows and/or Linux? (If it's there in Linux presumably it only works if there
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.