##// END OF EJS Templates
utils.data: Use list comprehension for uniq_stable()...
W. Trevor King -
Show More
@@ -1,106 +1,96
1 1 # encoding: utf-8
2 2 """Utilities for working with data structures like lists, dicts and tuples.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2008-2011 The IPython Development Team
7 7 #
8 8 # Distributed under the terms of the BSD License. The full license is in
9 9 # the file COPYING, distributed as part of this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 import types
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Code
20 20 #-----------------------------------------------------------------------------
21 21
22 22 def uniq_stable(elems):
23 23 """uniq_stable(elems) -> list
24 24
25 25 Return from an iterable, a list of all the unique elements in the input,
26 26 but maintaining the order in which they first appear.
27 27
28 A naive solution to this problem which just makes a dictionary with the
29 elements as keys fails to respect the stability condition, since
30 dictionaries are unsorted by nature.
31
32 Note: All elements in the input must be valid dictionary keys for this
33 routine to work, as it internally uses a dictionary for efficiency
34 reasons."""
35
36 unique = []
37 unique_dict = {}
38 for nn in elems:
39 if nn not in unique_dict:
40 unique.append(nn)
41 unique_dict[nn] = None
42 return unique
28 Note: All elements in the input must be hashable for this routine
29 to work, as it internally uses a set for efficiency reasons.
30 """
31 seen = set()
32 return [x for x in elems if x not in seen and not seen.add(x)]
43 33
44 34
45 35 def sort_compare(lst1, lst2, inplace=1):
46 36 """Sort and compare two lists.
47 37
48 38 By default it does it in place, thus modifying the lists. Use inplace = 0
49 39 to avoid that (at the cost of temporary copy creation)."""
50 40 if not inplace:
51 41 lst1 = lst1[:]
52 42 lst2 = lst2[:]
53 43 lst1.sort(); lst2.sort()
54 44 return lst1 == lst2
55 45
56 46
57 47 def list2dict(lst):
58 48 """Takes a list of (key,value) pairs and turns it into a dict."""
59 49
60 50 dic = {}
61 51 for k,v in lst: dic[k] = v
62 52 return dic
63 53
64 54
65 55 def list2dict2(lst, default=''):
66 56 """Takes a list and turns it into a dict.
67 57 Much slower than list2dict, but more versatile. This version can take
68 58 lists with sublists of arbitrary length (including sclars)."""
69 59
70 60 dic = {}
71 61 for elem in lst:
72 62 if type(elem) in (types.ListType,types.TupleType):
73 63 size = len(elem)
74 64 if size == 0:
75 65 pass
76 66 elif size == 1:
77 67 dic[elem] = default
78 68 else:
79 69 k,v = elem[0], elem[1:]
80 70 if len(v) == 1: v = v[0]
81 71 dic[k] = v
82 72 else:
83 73 dic[elem] = default
84 74 return dic
85 75
86 76
87 77 def flatten(seq):
88 78 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
89 79
90 80 return [x for subseq in seq for x in subseq]
91 81
92 82
93 83 def get_slice(seq, start=0, stop=None, step=1):
94 84 """Get a slice of a sequence with variable step. Specify start,stop,step."""
95 85 if stop == None:
96 86 stop = len(seq)
97 87 item = lambda i: seq[i]
98 88 return map(item,xrange(start,stop,step))
99 89
100 90
101 91 def chop(seq, size):
102 92 """Chop a sequence into chunks of the given size."""
103 93 chunk = lambda i: seq[i:i+size]
104 94 return map(chunk,xrange(0,len(seq),size))
105 95
106 96
General Comments 0
You need to be logged in to leave comments. Login now