Problem with itertools.groupby.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • trebucket@gmail.com

    #1

    Problem with itertools.groupby.

    What am I doing wrong here?
    [color=blue][color=green][color=darkred]
    >>> import operator
    >>> import itertools
    >>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),[/color][/color][/color]
    .... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)][color=blue][color=green][color=darkred]
    >>> for k, g in itertools.group by(iter(vals), operator.itemge tter(0)):[/color][/color][/color]
    .... print k, [i for i in g]
    ....
    1 [(1, 11)]
    2 [(2, 12)]
    3 [(3, 13)]
    4 [(4, 14)]
    5 [(5, 15)]
    1 [(1, 16)]
    2 [(2, 17)]
    3 [(3, 18)]
    4 [(4, 19)]
    5 [(5, 20)]

    What I want is tuples starting with identical numbers to be grouped. I
    cannot figure out why this is not working. If anyone has any insights,
    I would appreciate it.

    - Alex Ross

  • Scott David Daniels

    #2
    Re: Problem with itertools.group by.

    trebucket@gmail .com wrote:[color=blue]
    > What am I doing wrong here?
    >[color=green][color=darkred]
    >>>> import operator
    >>>> import itertools
    >>>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),[/color][/color]
    > ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)][color=green][color=darkred]
    >>>> for k, g in itertools.group by(iter(vals), operator.itemge tter(0)):[/color][/color]
    > ... print k, [i for i in g]
    > ...
    >
    > What I want is tuples starting with identical numbers to be grouped. I
    > cannot figure out why this is not working. If anyone has any insights,
    > I would appreciate it.
    >
    > - Alex Ross
    >[/color]
    Sort the list before using it.
    [color=blue][color=green][color=darkred]
    >>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),[/color][/color][/color]
    (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)][color=blue][color=green][color=darkred]
    >>> def first(pair):[/color][/color][/color]
    return pair[0][color=blue][color=green][color=darkred]
    >>> for k, g in itertools.group by(sorted(vals, key=first), first):[/color][/color][/color]
    print k, [i for i in g]

    "groupby" depends on the source stream having the clustering you need.
    Otherwise it could not work "on the fly" for arbitrarily large sources.
    Often you can arrange for your data source to be clustered; when you
    cannot, the groupby arg is a great sort key.

    --Scott David Daniels
    scott.daniels@a cm.org

    Comment

    • Fredrik Lundh

      #3
      Re: Problem with itertools.group by.

      trebucket@gmail .com wrote:
      [color=blue]
      > What am I doing wrong here?
      >[color=green][color=darkred]
      >>>> import operator
      >>>> import itertools
      >>>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),[/color][/color]
      > ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)][color=green][color=darkred]
      >>>> for k, g in itertools.group by(iter(vals), operator.itemge tter(0)):[/color][/color]
      > ... print k, [i for i in g]
      > ...
      > 1 [(1, 11)]
      > 2 [(2, 12)]
      > 3 [(3, 13)]
      > 4 [(4, 14)]
      > 5 [(5, 15)]
      > 1 [(1, 16)]
      > 2 [(2, 17)]
      > 3 [(3, 18)]
      > 4 [(4, 19)]
      > 5 [(5, 20)]
      >
      > What I want is tuples starting with identical numbers to be grouped. I
      > cannot figure out why this is not working. If anyone has any insights,
      > I would appreciate it.[/color]

      itertools only looks for changes to the key value (the one returned by
      operator.itemge tter(0) in your case); it doesn't sort the list for you.

      this should work:

      for k, g in itertools.group by(sorted(vals) , operator.itemge tter(0)):
      print k, [i for i in g]

      </F>

      Comment

      • Fredrik Lundh

        #4
        Re: Problem with itertools.group by.

        > itertools only looks for changes to the key value (the one returned by[color=blue]
        > operator.itemge tter(0) in your case); it doesn't sort the list for you.
        >
        > this should work:
        >
        > for k, g in itertools.group by(sorted(vals) , operator.itemge tter(0)):
        > print k, [i for i in g][/color]

        footnote: to turn the contents in an iterator into a list object,
        list(g) is a bit more convenient than [i for i in g].

        </F>

        Comment

        Working...