I'm starting to think like a Pythonista

B

brad

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]
 
E

Erik Jones

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®
(e-mail address removed)
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
 
B

Bjoern Schliessmann

brad said:
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
 
B

brad

Bjoern said:
brad said:
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.

What is the dis-advantage of using xrange over range in this circumstance?
 
B

brad

Erik said:
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]
 
M

Marc 'BlackJack' Rintsch

Bjoern said:
brad said:
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.

What is the dis-advantage of using xrange over range in this circumstance?

It's an unnecessary intermediate step.

Ciao,
Marc 'BlackJack' Rintsch
 
E

Erik Jones

Bjoern said:
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.

What is the dis-advantage of using xrange over range in this
circumstance?

It's an unnecessary intermediate step.

Exactly, there's no disadvantage, but the use case for xrange is for
when there is an advantage. xrange is a lazy generator, which means
that the values in the range are generated one by one, i.e. the
entire range is never present in memory all at once. If all your
doing is building that list then there's no advantage. In fact, [x
for x in xrange(99)] can be though of as longhand for range(99). Put
another way, xrange returns a generator of successive, individual
numbers over a range (optionally with a step) while range returns a
list of numbers over a range (optionally with a step).
<type 'list'>


Erik Jones

Software Developer | Emma®
(e-mail address removed)
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
 
B

Bjoern Schliessmann

brad said:
Bjoern Schliessmann wrote:

What is the dis-advantage of using xrange over range in this
circumstance?

Hardly any, but honestly: Converting it to a list manually is more
than superfluous.

Regards,


Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top