groupby로 list 내부에 그룹을 지어보자!

|

groupby import

from itertools import groupby

어떤 함수인가?

itertools.groupby(iterable[, key]) iterable을 받아서 연속된 키와 그룹을 리턴한다.

>>> from itertools import groupby
>>> things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")]
>>> group = groupby(things, lambda x: x[0])
>>> for key, items in group:
...     print(key)
...     for item in items:
...             print(item)
... 
animal
('animal', 'bear')
('animal', 'duck')
plant
('plant', 'cactus')
vehicle
('vehicle', 'speed boat')
('vehicle', 'school bus')

그런데 주의해야할 점이 존재한다. iterator를 돌면서 다른 key를 만나면 새 그룹을 만드는 식으로 동작하기 때문에, 같은 키라도 분산되어있으면 한 그룹에 뭉쳐지지 않는다. 다음은 소팅을 먼저 하고 grouping 시키는 wrapper를 만든 예제이다.

from itertools import groupby

def printThings(things, groupFunc):
    print("===================BEGIN===================")
    for key, group in groupFunc(things, lambda x: x[0]):
        for thing in group:
            print "A %s is a %s." % (thing[1], key)
        print " "
    print("====================END====================")

def myGroupby(iterable, func):
    tmpiterable = sorted(iterable, key=func)
    return groupby(tmpiterable, func)

things = [("animal", "bear"),
          ("vehicle", "speed boat"),
          ("animal", "duck"),
          ("plant", "cactus"),
          ("vehicle", "school bus")]
printThings(things, groupby)
printThings(things, myGroupby)
### OUTPUT ###
===================BEGIN===================
A bear is a animal.
 
A speed boat is a vehicle.
 
A duck is a animal.
 
A cactus is a plant.
 
A school bus is a vehicle.
 
====================END====================
===================BEGIN===================
A bear is a animal.
A duck is a animal.
 
A cactus is a plant.
 
A speed boat is a vehicle.
A school bus is a vehicle.
 
====================END====================