itertools.count() as built-in

Discussion in 'Python' started by jantod@gmail.com, May 28, 2006.

  1. Guest

    Is there any chance of itertools.count() ever becoming one of the
    built-in functions? It's a wonderful little function and I find myself
    importing it in every module I write.

    -Janto
     
    , May 28, 2006
    #1
    1. Advertising

  2. John Machin Guest

    On 29/05/2006 4:14 AM, wrote:
    > Is there any chance of itertools.count() ever becoming one of the
    > built-in functions? It's a wonderful little function and I find myself
    > importing it in every module I write.
    >


    Every module?? Do you use any/many other itertools functions? Care to
    give us a sample of your typical usage of itertools.count?

    Cheers,
    John
     
    John Machin, May 29, 2006
    #2
    1. Advertising

  3. Guest

    I have a project of around 6000 lines where I used count() 20 times. It
    has 14 modules, 10 of which I needed an explicit import.

    Many of the usages are of the form:

    for item, n in zip(items, count(N)):
    dostuff

    Around half of these are due to using pylab.subplot(x,y.n), which
    requires values for n>=1. So I want N=1. The enumerate builtin starts
    from 0.
    I also write out textual reports that start with n=1.

    I also have things like
    for n, component, rotations in zip(count(),
    sorted(tools.component_files().keys()), all_symbol_rotations)
    where the enumerate version just looks even more confusing and easy to
    mess up
    for n, component, rotations in
    enumerate(sorted(tools.component_files().keys()), all_symbol_rotations)

    Some more examples follow. Note that I find it more natural to specify
    the count last as it's usually less important than the other values.
    This is different from the order given by enumerate, so I'm more
    inclined to use count().

    mutated_symbols_and_names = zip(mutated_symbols, (("<%s mutation %s>" %
    (component, m)) for m in count()))
    edge_weight=dict(zip(Intersection.types, count(1)))
    for (component, filename), n in zip(components, count()):
    for (component, filename), component_count in
    zip(tools.component_files().items(), count()):
     
    , May 29, 2006
    #3
  4. Guest

    Oh and I use repeat, chain and cycle quite a bit. But I don't mind
    importing them as they are usually limited to one module.
     
    , May 29, 2006
    #4
  5. <> wrote:

    > I have a project of around 6000 lines where I used count() 20 times. It
    > has 14 modules, 10 of which I needed an explicit import.
    >
    > Many of the usages are of the form:
    >
    > for item, n in zip(items, count(N)):
    > dostuff
    >
    > Around half of these are due to using pylab.subplot(x,y.n), which
    > requires values for n>=1. So I want N=1. The enumerate builtin starts
    > from 0.


    Hmmm, makes me wonder if enumerate should grow optional arguments for
    starting and stepping... I've found myself in similar situations, using
    "x*i+y" on the i returned by enumerate, or zipping an xrange instead...


    Alex
     
    Alex Martelli, May 29, 2006
    #5
  6. John Machin Guest

    On 29/05/2006 9:50 AM, wrote:
    > I have a project of around 6000 lines where I used count() 20 times. It
    > has 14 modules, 10 of which I needed an explicit import.
    >
    > Many of the usages are of the form:
    >
    > for item, n in zip(items, count(N)):
    > dostuff
    >
    > Around half of these are due to using pylab.subplot(x,y.n), which
    > requires values for n>=1. So I want N=1. The enumerate builtin starts
    > from 0.


    Call me a Luddite if you will, but I'd suggest that instead of
    for item, n in zip(items, count(N)):
    dostuff(item, n)
    you try
    (1)
    for n, item in enumerate(items):
    dostuff(item, n+N)
    or (2)
    n = N-1
    for item in items:
    n += 1
    dostuff(item, n)

    > I also write out textual reports that start with n=1.
    >
    > I also have things like
    > for n, component, rotations in zip(count(),
    > sorted(tools.component_files().keys()), all_symbol_rotations)
    > where the enumerate version just looks even more confusing and easy to
    > mess up
    > for n, component, rotations in
    > enumerate(sorted(tools.component_files().keys()), all_symbol_rotations)


    As enumerate takes only 1 argument, I presume that is the messed-up version.

    Yes, the correct version is a pain:
    for n, (component, rotations) in enumerate(zip(yadda1, yadda2)):

    Perhaps you could use something like this:

    >>> def zipwithcount(N, *args):

    .... for x, guff in enumerate(zip(*args)):
    .... yield guff + (x + N,)
    ....
    >>> list(zipwithcount(666, 'abc', 'xyz'))

    [('a', 'x', 666), ('b', 'y', 667), ('c', 'z', 668)]
    >>>


    Cheers,
    John
     
    John Machin, May 29, 2006
    #6
  7. Guest

    Oops. The messed-up version wasn't supposed to be messed-up. Two
    mistakes on one line. Which kinda proves my point :)

    I'd much rather use the count version than (1) or (2). (1) has the
    problem of having "incorrect" values the rest of the time in the loop
    and (2) is going to an extreme just to avoid an import of count.

    Your zipwithcount doesn't look as obvious as
    for n, a, b in zip(count(), A, B)
    but is still easier to read than
    for n, (a, b) in enumerate(zip(A, B))

    -Janto
     
    , May 29, 2006
    #7
  8. Guest

    Zipping an xrange? I'm having trouble visualizing how you do that to
    avoid x*i+y.

    -Janto
     
    , May 29, 2006
    #8
  9. Duncan Smith Guest

    wrote:
    > Zipping an xrange? I'm having trouble visualizing how you do that to
    > avoid x*i+y.
    >
    > -Janto
    >


    Something like,

    >>> lis = ['a', 'b', 'c', 'd']
    >>> y = 3
    >>> i = 7
    >>> for n, item in zip(xrange(y, len(lis)*i+y, i), lis):

    print n, item


    3 a
    10 b
    17 c
    24 d
    >>>


    Duncan
     
    Duncan Smith, May 29, 2006
    #9
  10. Duncan Smith <> wrote:

    > wrote:
    > > Zipping an xrange? I'm having trouble visualizing how you do that to
    > > avoid x*i+y.
    > >
    > > -Janto
    > >

    >
    > Something like,
    >
    > >>> lis = ['a', 'b', 'c', 'd']
    > >>> y = 3
    > >>> i = 7
    > >>> for n, item in zip(xrange(y, len(lis)*i+y, i), lis):

    > print n, item


    Actually I tend to use sys.maxint as the xrange's middle arg, but, yes,
    this IS the general idea!-)


    Alex
     
    Alex Martelli, May 29, 2006
    #10
  11. wrote:
    > Is there any chance of itertools.count() ever becoming one of the
    > built-in functions?


    That's unlikely. The goal is to have fewer builtins rather than more.
    Utility and frequency are not the only considerations; otherwise
    glob.glob, sys.stderr, print.pprint, copy.copy, and many others would
    also be candidates.


    > It's a wonderful little function and I find myself
    > importing it in every module I write.


    I'm glad you find it so useful.


    Raymond
     
    Raymond Hettinger, May 29, 2006
    #11
    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. Steven Bethard
    Replies:
    0
    Views:
    406
    Steven Bethard
    Mar 12, 2005
  2. itertools.count(-3)

    , Sep 21, 2006, in forum: Python
    Replies:
    2
    Views:
    338
    Jack Diederich
    Sep 21, 2006
  3. Robbe Morris - [MVP] C#

    ASP.NET sites with built-in max session count governors?

    Robbe Morris - [MVP] C#, Sep 27, 2007, in forum: ASP .Net
    Replies:
    9
    Views:
    465
    Mark Rae [MVP]
    Sep 27, 2007
  4. Raymond Hettinger
    Replies:
    17
    Views:
    559
    Simon Brunning
    Feb 18, 2008
  5. Nick Mellor
    Replies:
    35
    Views:
    377
    Paul Rubin
    Dec 6, 2012
Loading...

Share This Page