itertools.groupby
BJörn Lindqvist
bjourne at gmail.com
Tue Jun 5 15:00:40 EDT 2007
On 27 May 2007 10:49:06 -0700, 7stud <bbxx789_05ss at yahoo.com> wrote:
> On May 27, 11:28 am, Steve Howell <showel... at yahoo.com> wrote:
> > The groupby method has its uses, but it's behavior is
> > going to be very surprising to anybody that has used
> > the "group by" syntax of SQL, because Python's groupby
> > method will repeat groups if your data is not sorted,
> > whereas SQL has the luxury of (knowing that it's)
> > working with a finite data set, so it can provide the
> > more convenient semantics.
> > The groupby method has its uses
>
> I'd settle for a simple explanation of what it does in python.
Here is another example:
import itertools
import random
dierolls = sorted(random.randint(1, 6) for x in range(200))
for number, numbers in itertools.groupby(dierolls):
number_count = len(list(numbers))
print number, "came up", number_count, "times."
--
mvh Björn
More information about the Python-list
mailing list