Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> list(itertools.groupby('LLLLAAGGG'))
- [('L', <itertools._grouper object at 0x109cf0208>), ('A', <itertools._grouper object at 0x109cf0128>), ('G', <itertools._grouper object at 0x109cf0240>)]
- >>> for char, group in itertools.groupby('LLLLAAAGG'):
- ... print(char, '->', list(group))
- ...
- L -> ['L', 'L', 'L', 'L']
- A -> ['A', 'A', 'A']
- G -> ['G', 'G']
- >>> animals = ['duck', 'eagle', 'rat', 'giraffe', 'bear','bat', 'dolphin', 'shark', 'lion']
- >>> animals.sort(key= len)
- >>> animals
- ['rat', 'bat', 'duck', 'bear', 'lion', 'eagle', 'shark', 'giraffe', 'dolphin']
- >>> for length, group in itertools.groupby(animals, len):
- ... print(length, '->', list(group))
- ...
- 3 -> ['rat', 'bat']
- 4 -> ['duck', 'bear', 'lion']
- 5 -> ['eagle', 'shark']
- 7 -> ['giraffe', 'dolphin']
- >>> for length, group in itertools.groupby(reversed(animals), len):
- ... print(length, '->', list(group))
- ...
- 7 -> ['dolphin', 'giraffe']
- 5 -> ['shark', 'eagle']
- 4 -> ['lion', 'bear', 'duck']
- 3 -> ['bat', 'rat']
- >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement