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