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