473,782 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I'm starting to think like a Pythonista

I was looking at a way to implement Ruby's upto method in python. I came
up with the code below... three years ago, I would never have thought of
list comprehension, today it seems second nature. This may be totally
un-Pythonic, but I thought it was kind of clever. Man, for some reason,
I feel like being rude and lofty acting :)

low_odds = [1,3,5,7,9]
# make a list containing 10 - 98 evens only
big_evens = big_evens = [x for x in list(xrange(99) ) if x % 2 == 0 and
x >8]

low_evens = [2,4,6,8]
# make a list containing 11 - 99 odds only
big_odds = [x for x in list(xrange(100 )) if x % 2 != 0 and x >9]

y = 8

if y in low_evens:
ok_numbers = low_odds + big_evens + [x for x in low_evens if x <= y]
Oct 10 '07 #1
3 1244

On Oct 10, 2007, at 2:51 PM, brad wrote:
I was looking at a way to implement Ruby's upto method in python. I
came
up with the code below... three years ago, I would never have
thought of
list comprehension, today it seems second nature. This may be totally
un-Pythonic, but I thought it was kind of clever. Man, for some
reason,
I feel like being rude and lofty acting :)

low_odds = [1,3,5,7,9]
# make a list containing 10 - 98 evens only
big_evens = big_evens = [x for x in list(xrange(99) ) if x % 2 ==0
and
x >8]
big_evens = range(10, 100, 2)
>
low_evens = [2,4,6,8]
# make a list containing 11 - 99 odds only
big_odds = [x for x in list(xrange(100 )) if x % 2 != 0 and x >9]
big_odds = range(11, 100, 2)
Erik Jones

Software Developer | Emma®
er**@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Oct 10 '07 #2
brad wrote:
low_odds = [1,3,5,7,9]
# make a list containing 10 - 98 evens only
big_evens = big_evens = [x for x in list(xrange(99) ) if x % 2 ==
0 and x >8]
Why use xrange if you convert it to a full list in place? No
advantage there.

Regards,
Björn
--
BOFH excuse #300:

Digital Manipulator exceeding velocity parameters

Oct 10 '07 #3
Erik Jones wrote:
big_evens = range(10, 100, 2)
big_odds = range(11, 100, 2)
Neat, but not as clever or as hard to read as mine... I'll bet it faster
though... maybe not.

The upto part is here:

ok_numbers = low_odds + big_evens + [x for x in low_evens if x <= y]

Oct 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1164
by: David Goodger | last post by:
With apologies in advance to the devoutly religious among us, who may be offended by this corruption of The Lord's Prayer. No offence intended. The Pythonista's Prayer ======================= Our language, Which art in programs, Python be thy Name.
12
2954
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am starting is that it has a welcome window first which gets displayed and then the real windows after a while,in other words it means that the process name is the same, but the handle I need to retrive is the one from the final window and not the...
0
3202
by: Max | last post by:
Hi Folks, I'm received this error on starting Control Center from the Start menu.... db2javit.dll This file does not have a program associated with it for performing this action. Create an association in My Computer by clicking View and then Clicking Options. Here's what I've tried....
5
1081
by: Sjaakie Helderhorst | last post by:
Hi! I use an ISDN monitor which displays caller ID. In stead of entering all numbers manually, I wish to query a national phonenumber database, which I obtained as a MySQL database. The only way I can pass the caller id from the software to another app, is by executing it with a parameter (this parameter is the phonenumber). So far I managed to do so, I wrote a vb console app which queries the database and writes output to a text-file...
1
1545
by: juleni | last post by:
Hello, is there a possibility starting or stopping postgres database and creating database instance by JAVA? Is there some API available for this? If yes, can you please write some example, how to do it? Thank you in advance, with best regards,
10
28619
by: Andrew Bullock | last post by:
Hi, code: myClass x = new myClass(); x.dosomethingwith(x,y); How do i make dosomethingwith(x,y) run on a separate thread, and then inform the main thread when it has finished?
6
4206
by: Arnie | last post by:
We're using the ServiceController class provided by the .NET Framework, programming in C#. We are using the Start() method to start a service from another service. This works fine most of the time, but occasionally after a system startup (where the service doing the starting of the other is automatically started by Windows XP) the ServiceController's Start() method fails to start the other service. It is probably throwing an...
16
3386
by: Jim Langston | last post by:
I know that functions starting with an underscore, or two underscores, are reserved by the compiler/c++ and should not be used by the user and may cause undefined behavior. My question is, how likely is it to actually cause undefined behavior? The reason I'm asking is I'm using a game engine where the sockets code is not working correctly on my computer, but seems to work correctly on everyone elses. I am not compiling the dll myself,...
7
2392
by: | last post by:
Hi to everyone! I have an Apache Webserver running on Win2000. I try to start a console application an the server though PHP, with the functions exec() or passthru() but it doesn't work. The application starts though, but doesn't work in the same way I would start it local on the web server (I mean though a "normal doubleclick"). I think it depends on the type of application, cause I ca start with PHP for example other programms and...
1
1856
by: ropo | last post by:
I have a .NET 2.0 app that at one point starts an old MFC App through System.Diagnostics.Process.Start from a model form/ I then wait for it to finish by calling StartedProcess.WaitForExit(); Problem is: When I close the MFC app if there is an window behind my .NET app it will come to the front leaving my .NET app at the back.
0
9639
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
10311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
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
9942
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
8967
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...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
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();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.