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]
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: jbailo |
last post by:
and it sounds GREAT !!!
http://www.virginradio.co.uk/thestation/listen/ogg.html
OGG
OGG
OGG
!
!
!
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
|
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: 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,...
| |