Velocity Reviews > multiple discontinued ranges

# multiple discontinued ranges

xoff
Guest
Posts: n/a

 11-10-2010
I was wondering what the best method was in Python programming for 2
discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.
Am I obliged to use 2 for loops defining the 2 ranges like this:

for i in range (3,7):
do bla
for i in range (7,17):
do bla

or is there a more clever way to do this?

Paul Rudin
Guest
Posts: n/a

 11-10-2010
xoff <(E-Mail Removed)> writes:

> I was wondering what the best method was in Python programming for 2
> discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.
> Am I obliged to use 2 for loops defining the 2 ranges like this:
>
> for i in range (3,7):
> do bla
> for i in range (7,17):
> do bla
>
> or is there a more clever way to do this?

for i in itertools.chain(xrange(3,7), xrange(17,23)):
print i

Paul Rubin
Guest
Posts: n/a

 11-10-2010
xoff <(E-Mail Removed)> writes:
> I was wondering what the best method was in Python programming for 2
> discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.

you could use itertools.chain:

from itertools import chain

for i in chain(range(3,7), range(17,23)):
...

I'm assuming you're using python 3. In python 2 each of those ranges
expands immediately to a list, so on the one hand they're consuming
potentially lots of storage (you should use xrange instead). On the
other hand you could just concatenate the lists with +, but I wouldn't

xoff
Guest
Posts: n/a

 11-10-2010
On 10 nov, 18:13, Paul Rudin <(E-Mail Removed)> wrote:
> xoff <(E-Mail Removed)> writes:
> > I was wondering what the best method was in Python programming for 2
> > discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.
> > Am I obliged to use 2 for loops defining the 2 ranges like this:

>
> > for i in range (3,7):
> > *do bla
> > for i in range (7,17):
> > *do bla

>
> > or is there a more clever way to do this?

>
> for i in itertools.chain(xrange(3,7), xrange(17,23)):
> * * print i

Thank you, that was exactly what I needed!

xoff
Guest
Posts: n/a

 11-10-2010
On 10 nov, 18:15, Paul Rubin <(E-Mail Removed)> wrote:
> you could use itertools.chain:
>
> * from itertools import chain
>
> * for i in chain(range(3,7), range(17,23)):
> * * ...
>
> I'm assuming you're using python 3. *In python 2 each of those ranges
> expands immediately to a list, so on the one hand they're consuming
> potentially lots of storage (you should use xrange instead). *On the
> other hand you could just concatenate the lists with +, but I wouldn't

I am curious, why wouldn't you advise something like this:
for i in chain(range(3,7) + range(17,23)):

Thanks again!

Mel
Guest
Posts: n/a

 11-10-2010
xoff wrote:

> I was wondering what the best method was in Python programming for 2
> discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.
> Am I obliged to use 2 for loops defining the 2 ranges like this:
>
> for i in range (3,7):
> do bla
> for i in range (7,17):
> do bla
>
> or is there a more clever way to do this?

One horribly clever way is to concoct a 9-th order polynomial to return
3,4,5,6,17,18,19,20,21,22 for input values 0,1,2,3,4,5,6,7,8,9.

The reasonable way is to use two loops as you've done. If the pattern of
discontinuous ranges is really important in your application, you'd perhaps
want to package it up (not tested):

def important_range ():
for x in xrange (3, 7):
yield x
for x in xrange (17,23):
yield x

to be used elsewhere as

for v in important_range():
# use v ...

Mel.

Peter Otten
Guest
Posts: n/a

 11-10-2010
xoff wrote:

> I was wondering what the best method was in Python programming for 2
> discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.
> Am I obliged to use 2 for loops defining the 2 ranges like this:
>
> for i in range (3,7):
> do bla
> for i in range (7,17):
> do bla
>
> or is there a more clever way to do this?

>>> for r in xrange(3, 7), xrange(17, 23):

.... for i in r:
.... print i,
....
3 4 5 6 17 18 19 20 21 22

Nobody
Guest
Posts: n/a

 11-10-2010
On Wed, 10 Nov 2010 09:34:14 -0800, xoff wrote:

> I am curious, why wouldn't you advise something like this:
> for i in chain(range(3,7) + range(17,23)):

Because it constructs all three lists (both of the individual ranges and
their concatenation) in memory. For a trivial example, that isn't a
problem; for a real application, it could be.

Emile van Sebille
Guest
Posts: n/a

 11-10-2010
On 11/10/2010 9:34 AM xoff said...
> On 10 nov, 18:15, Paul Rubin<(E-Mail Removed)> wrote:
>> potentially lots of storage (you should use xrange instead). On the
>> other hand you could just concatenate the lists with +, but I wouldn't

>
> I am curious, why wouldn't you advise something like this:
> for i in chain(range(3,7) + range(17,23)):

I'd assume because concatenation is generally considered expensive.

If you were to do it, you'd likely drop the chain and write:

for i in range(3,7) + range(17,23):

Emile

MRAB
Guest
Posts: n/a

 11-10-2010
On 10/11/2010 17:34, xoff wrote:
> On 10 nov, 18:15, Paul Rubin<(E-Mail Removed)> wrote:
>> you could use itertools.chain:
>>
>> from itertools import chain
>>
>> for i in chain(range(3,7), range(17,23)):
>> ...
>>
>> I'm assuming you're using python 3. In python 2 each of those ranges
>> expands immediately to a list, so on the one hand they're consuming
>> potentially lots of storage (you should use xrange instead). On the
>> other hand you could just concatenate the lists with +, but I wouldn't

>
> I am curious, why wouldn't you advise something like this:
> for i in chain(range(3,7) + range(17,23)):
>

In Python 3 'range' is a generator, like 'xrange' in Python 2.