whatsnew 2.4 about itertools.groupby:

G

G?nter Jantzen

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'
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top