itertools.groupby() not grouping correctly

后端 未结 3 1329
北恋
北恋 2020-11-28 13:53

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)]
         


        
3条回答
  •  醉酒成梦
    2020-11-28 14:16

    Variant without sorting (via dictionary). Should be better performance-wise.

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

提交回复
热议问题