itertools.groupby () groups incorrectly

I have this data:

self.data = [(1, 1, 5.0),
             (1, 2, 3.0),
             (1, 3, 4.0),
             (2, 1, 4.0),
             (2, 2, 2.0)]

When I run this code:

for mid, group in itertools.groupby(self.data, key=operator.itemgetter(0)):

for list(group)i get:

[(1, 1, 5.0),
 (1, 2, 3.0),
 (1, 3, 4.0)]

what i want

But if I use 1 instead of 0

for mid, group in itertools.groupby(self.data, key=operator.itemgetter(1)):

To group by the second number in tuples, I get only:

[(1, 1, 5.0)]

even if there are other tuples that have a “1” in that 1 (2nd) position.

+10
source share
3 answers

itertools.groupby brings together adjacent elements with the same key. If you want all items with the same key, you need to sort first self.data.

for mid, group in itertools.groupby(
    sorted(self.data,key=operator.itemgetter(1)), key=operator.itemgetter(1)):
+17
source

Option without sorting (via dictionary). Should be more efficient in performance.

def full_group_by(l, key=lambda x: x):
    d = defaultdict(list)
    for item in l:
        d[key(item)].append(item)
    return d.items()
+14

"" Python itertools.groupby.

def groupby2(l, key=lambda x:x, val=lambda x:x, agg=lambda x:x, sort=True):
    if sort:
        l = sorted(l, key=key)
    return ((k, agg((val(x) for x in v))) \
        for k,v in itertools.groupby(l, key=key))

,

  1. .
  2. , key .
  3. tuple(key, grouped_values) 3- .
  4. , sum avg.

import itertools
from operator import itemgetter
from statistics import *

t = [('a',1), ('b',2), ('a',3)]
for k,v in groupby2(t, itemgetter(0), itemgetter(1), sum):
  print(k, v)

,

a 4
b 2

0

All Articles