Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. >>> list(itertools.groupby('LLLLAAGGG'))
  2. [('L', <itertools._grouper object at 0x109cf0208>), ('A', <itertools._grouper object at 0x109cf0128>), ('G', <itertools._grouper object at 0x109cf0240>)]
  3. >>> for char, group in itertools.groupby('LLLLAAAGG'):
  4. ... print(char, '->', list(group))
  5. ...
  6. L -> ['L', 'L', 'L', 'L']
  7. A -> ['A', 'A', 'A']
  8. G -> ['G', 'G']
  9. >>> animals = ['duck', 'eagle', 'rat', 'giraffe', 'bear','bat', 'dolphin', 'shark', 'lion']
  10. >>> animals.sort(key= len)
  11. >>> animals
  12. ['rat', 'bat', 'duck', 'bear', 'lion', 'eagle', 'shark', 'giraffe', 'dolphin']
  13. >>> for length, group in itertools.groupby(animals, len):
  14. ... print(length, '->', list(group))
  15. ...
  16. 3 -> ['rat', 'bat']
  17. 4 -> ['duck', 'bear', 'lion']
  18. 5 -> ['eagle', 'shark']
  19. 7 -> ['giraffe', 'dolphin']
  20. >>> for length, group in itertools.groupby(reversed(animals), len):
  21. ... print(length, '->', list(group))
  22. ...
  23. 7 -> ['dolphin', 'giraffe']
  24. 5 -> ['shark', 'eagle']
  25. 4 -> ['lion', 'bear', 'duck']
  26. 3 -> ['bat', 'rat']
  27. >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement