Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Thursday, May 3, 2012

Itertools groupby method explained

Itertools groupby makes an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element. If not specified or is None, key defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.

Caution:The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list.
from itertools import groupby

text = """bear mammal
lion mammal
hawk bird
lizzard reptile
crocodile reptile"""

animals = [(name, type) for name, type in (line.split() for line in text.split('\n'))]

for key, group in groupby(animals, lambda x: x[1]):
    #The first group looks like [('bear', 'mammal'), ('lion', 'mammal')]
    animalerPerType = ', '.join(name for name, type in group)
    print "%s are %s." % (animalerPerType , key + 's')

bear, lion are mammals.
hawk are birds.
lizzard, crocodile are reptiles.

No comments:

Post a Comment