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