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