data.py 1015 B

123456789101112131415161718192021222324252627282930
  1. # encoding: utf-8
  2. """Utilities for working with data structures like lists, dicts and tuples.
  3. """
  4. #-----------------------------------------------------------------------------
  5. # Copyright (C) 2008-2011 The IPython Development Team
  6. #
  7. # Distributed under the terms of the BSD License. The full license is in
  8. # the file COPYING, distributed as part of this software.
  9. #-----------------------------------------------------------------------------
  10. def uniq_stable(elems):
  11. """uniq_stable(elems) -> list
  12. Return from an iterable, a list of all the unique elements in the input,
  13. but maintaining the order in which they first appear.
  14. Note: All elements in the input must be hashable for this routine
  15. to work, as it internally uses a set for efficiency reasons.
  16. """
  17. seen = set()
  18. return [x for x in elems if x not in seen and not seen.add(x)]
  19. def chop(seq, size):
  20. """Chop a sequence into chunks of the given size."""
  21. return [seq[i:i+size] for i in range(0,len(seq),size)]