Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > multiple discontinued ranges

Reply
Thread Tools

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?
 
Reply With Quote
 
 
 
 
Paul Rudin
Guest
Posts: n/a
 
      11-10-2010
xoff <> 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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      11-10-2010
xoff <> 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
advise that.
 
Reply With Quote
 
xoff
Guest
Posts: n/a
 
      11-10-2010
On 10 nov, 18:13, Paul Rudin <paul.nos...@rudin.co.uk> wrote:
> xoff <igor.idziejc...@gmail.com> 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!
 
Reply With Quote
 
xoff
Guest
Posts: n/a
 
      11-10-2010
On 10 nov, 18:15, Paul Rubin <no.em...@nospam.invalid> 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
> advise that.


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

Thanks again!
 
Reply With Quote
 
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.
 
Reply With Quote
 
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

 
Reply With Quote
 
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.

 
Reply With Quote
 
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<no.em...@nospam.invalid> 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
>> advise that.

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

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      11-10-2010
On 10/11/2010 17:34, xoff wrote:
> On 10 nov, 18:15, Paul Rubin<no.em...@nospam.invalid> 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
>> advise that.

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using multiple ranges paulk VHDL 0 10-02-2009 02:38 PM
DHCP multiple ip ranges Maarten MCTS 4 05-27-2008 10:10 PM
extract multiple ranges from a list pi.arctan@gmail.com Python 3 03-08-2008 10:27 PM
Multiple ISPs and Multiple IP Ranges from Each ISP Chennak Cisco 10 06-08-2005 09:29 PM
PIX-501 with multiple outside IP ranges Mike Ruskai Cisco 3 02-14-2005 11:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57