multiple discontinued ranges

Discussion in 'Python' started by xoff, Nov 10, 2010.

  1. xoff

    xoff Guest

    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?
     
    xoff, Nov 10, 2010
    #1
    1. Advertising

  2. xoff

    Paul Rudin Guest

    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
     
    Paul Rudin, Nov 10, 2010
    #2
    1. Advertising

  3. xoff

    Paul Rubin Guest

    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.
     
    Paul Rubin, Nov 10, 2010
    #3
  4. xoff

    xoff Guest

    On 10 nov, 18:13, Paul Rudin <> wrote:
    > 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


    Thank you, that was exactly what I needed!
     
    xoff, Nov 10, 2010
    #4
  5. xoff

    xoff Guest

    On 10 nov, 18:15, Paul Rubin <> 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!
     
    xoff, Nov 10, 2010
    #5
  6. xoff

    Mel Guest

    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.
     
    Mel, Nov 10, 2010
    #6
  7. xoff

    Peter Otten Guest

    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
     
    Peter Otten, Nov 10, 2010
    #7
  8. xoff

    Nobody Guest

    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.
     
    Nobody, Nov 10, 2010
    #8
  9. On 11/10/2010 9:34 AM xoff said...
    > On 10 nov, 18:15, Paul Rubin<> 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
     
    Emile van Sebille, Nov 10, 2010
    #9
  10. xoff

    MRAB Guest

    On 10/11/2010 17:34, xoff wrote:
    > On 10 nov, 18:15, Paul Rubin<> 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.
     
    MRAB, Nov 10, 2010
    #10
  11. xoff

    Paul Rubin Guest

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


    First of all, the outer chain does nothing. Second, concatenating the
    two lists creates a new list, consuming storage and taking time copying
    all the elements. Third, if there are n lists instead of two, I think
    the copying time is O(n**2) in the number of lists because there are n-1
    separate copy operations and they keep getting larger.
     
    Paul Rubin, Nov 10, 2010
    #11
  12. xoff

    Guest

    On Nov 10, 10:02 am, Mel <> wrote:
    > 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.
    >


    And one would want one with integer coefficients... truly horribly
    clever! A good example of complex is better than complicated...

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


    Reasonable; but DRY: don't repeat yourself. IMNSHO, better would be:

    .... def important_range ():
    .... for x in [xrange (3, 7), xrange (17,23)]:
    .... for y in x:
    .... yield y

    because face it, you're probably going to add to that list of ranges
    anyway, so you should anticipate it (and there's no cost if you don't
    end up adding to it).

    To extend that notion, I would imagine that itertools.chain is already
    basically defined as:

    .... def chain(*args):
    .... for x in args:
    .... for y in x:
    .... yield y

    so important_range() can then be simply defined as:

    .... def important_range:
    .... return chain(xrange (3, 7), xrange (17,23))

    Cheers - Chas

    > to be used elsewhere as
    >
    > for v in important_range():
    > # use v ...
    >


    > Mel.
     
    , Nov 11, 2010
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Blondie
    Replies:
    3
    Views:
    873
    HarryS
    Mar 7, 2004
  2. Replies:
    3
    Views:
    259
  3. paulk

    using multiple ranges

    paulk, Oct 2, 2009, in forum: VHDL
    Replies:
    0
    Views:
    499
    paulk
    Oct 2, 2009
  4. Galin Iliev

    IE Web Controls - discontinued ???

    Galin Iliev, Apr 11, 2005, in forum: ASP .Net Web Controls
    Replies:
    6
    Views:
    387
    amber
    Apr 26, 2005
  5. mgorkani

    dime being discontinued

    mgorkani, Apr 13, 2004, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    134
    Dave Bettin
    Apr 13, 2004
Loading...

Share This Page