473,387 Members | 1,528 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,387 software developers and data experts.

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 1230

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
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 =======================...
12
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...
0
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...
5
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...
1
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...
10
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
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...
16
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...
7
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...
1
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(); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.