whatsnew 2.4 about itertools.groupby:

Discussion in 'Python' started by G?nter Jantzen, Jun 9, 2004.

  1. In the documentation
    http://www.python.org/dev/doc/devel/whatsnew/node7.html is written
    about itertools.groupby:

    """Like it SQL counterpart, groupby() is typically used with sorted
    input."""

    In SQL queries is the groupby clause not related to 'input order'.
    This notion makes not much sense in SQL context.
    SQL is based on relational Algebra. A SQL- table is based on an
    unordered set of rows (implementation can be different, of course).

    So the analogon of
    ----------------------
    >>> import itertools
    >>> L = [2,4,6, 7,8,9,11, 12, 14]
    >>> for key_val, it in itertools.groupby(L, lambda x: x % 2):

    .... print key_val, list(it)
    ....
    0 [2, 4, 6]
    1 [7]
    0 [8]
    1 [9, 11]
    0 [12, 14]
    >>>

    ------------------------

    Say you have a table 'example' with only one column 'i'

    _________________________
    select * from example;

    I
    ----
    2
    14
    6
    7
    8
    9
    11
    12
    4
    ___________________________

    the order of rows is not defined

    Then you can group this table

    ____________________________________________
    select count(i), mod(i,2) from example group by mod(i,2)


    COUNT(I) | MOD(I,2)
    ---------+---------
    6 | 0
    3 | 1
    ___________________________________________

    The result dos not depend on 'input order' or 'runs'
     
    G?nter Jantzen, Jun 9, 2004
    #1
    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. Replies:
    3
    Views:
    330
    Fredrik Lundh
    May 25, 2006
  2. 7stud

    itertools.groupby

    7stud, May 27, 2007, in forum: Python
    Replies:
    13
    Views:
    597
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
    Jun 5, 2007
  3. Steve Howell

    Re: itertools.groupby

    Steve Howell, May 27, 2007, in forum: Python
    Replies:
    13
    Views:
    543
  4. Tobiah

    itertools.groupby

    Tobiah, Jan 15, 2008, in forum: Python
    Replies:
    2
    Views:
    306
    Tobiah
    Jan 16, 2008
  5. Kitlbast

    bug with itertools.groupby?

    Kitlbast, Oct 7, 2009, in forum: Python
    Replies:
    6
    Views:
    385
    Dave Angel
    Oct 7, 2009
Loading...

Share This Page