-
Notifications
You must be signed in to change notification settings - Fork 89
/
arrays.py
428 lines (322 loc) · 12.6 KB
/
arrays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""Arrays
"""
from __future__ import absolute_import
from collections import Iterable
from bisect import bisect_left
from ._compat import string_types, _range
from .utils import (
_make_callback,
_iter_callback,
_iter_unique_set,
_iter_unique
)
def compact(array):
"""Creates a list with all falsey values of array removed.
:param list array: list to compact
:rtype: list
"""
return [item for item in array if item]
def difference(array, *lists):
"""Creates a list of list elements not present in the other lists
:param list array: the list to process
:param list lists: lists to check
:rtype: list
"""
return (list(difference(set(array).difference(lists[0]),
*lists[1:])) if lists
else array)
def find_index(array, callback):
"""This method is similar to _.find, except that it returns the index of
the element that passes the callback check, instead of the element itself.
:param list array: list to process
:param function callback: filter function or where dict
:rtype: list
"""
n = -1
for is_true, _, i, _ in _iter_callback(array, callback):
if is_true:
n = i
break
return n
def find_last_index(array, callback):
"""This method is similar to _.find_index, except that it iterates over
elements from right to left.
:param list array: list to process
:param function callback: filter function or where dict
:rtype: list
"""
n = find_index(reversed(array), callback)
if n is not -1:
n = len(array) - n - 1
return n
def first(array, callback=None):
"""Gets the first element of the array. If a number n is passed, the first
n elements of the array are returned. If a callback function is passed,
elements at the beginning of the array are returned as long as the callback
returns truthy. The callback is invoked with three arguments:
(value, index, array). If a property name is passed for callback, the
created "_.pluck" style callback will return the property value of the
given element. If an object is passed for callback, the created "_.where"
style callback will return true for elements that have the properties of
the given object, else false.
:param list array: list to select from
:param mixed callback: callback to filter array
:rtype: mixed
"""
n = 0
for is_true, _, _, _ in _iter_callback(array, callback):
if is_true:
n += 1
else:
break
ret = array[:n]
if callback in (None, 1):
ret = ret[0]
return ret
head = first
take = first
def flatten(array, callback=None, _depth=0):
"""Flattens a nested array (the nesting can be to any depth). If callback
is True, array will only be flattened a single level. If callback is
passed, each element of array is passed through a callback before
flattening.
:param list array: list to flatten
:param mixed callback: callback to filter array
:rtype: list
"""
shallow = False
if callback is True:
shallow = True
elif callback:
cbk = _make_callback(callback)
array = [cbk(item) for item in array]
callback = None
lst = []
if all([isinstance(array, Iterable),
not isinstance(array, string_types),
not (shallow and _depth > 1)]):
for arr in array:
lst.extend(flatten(arr, callback, _depth + 1))
else:
lst.append(array)
return lst
def index_of(array, value, from_index=0):
"""Gets the index at which the first occurrence of value is found.
:param list array: list to search
:param mixed value: value to search for
:param integer from_index: the index to search from
:rtype: integer
"""
try:
return array.index(value, from_index)
except ValueError:
return False
def initial(array, callback=1):
"""Gets all but the last element of array. If a number n is passed, the
last n elements are excluded from the result. If a callback function is
passed, elements at the end of the array are excluded from the result as
long as the callback returns truthy.
:param list array: list to query
:param mixed callback: The function called per element or the number of
elements to exclude
"""
lst = array[::-1]
n = len(array)
for is_true, _, _, _ in _iter_callback(lst, callback):
if is_true:
n -= 1
else:
break
ret = array[:n]
return ret
def intersection(*arrays):
"""Computes the intersection of all the passed-in arrays using strict
equality for comparisons, i.e. ===.
:param list *arrays: arrays to process
:rtype: list
"""
return list(set(arrays[0]).intersection(*arrays))
def last(array, callback=None):
"""Gets the last element of the array. If a number n is passed, the last n
elements of the array are returned. If a callback function is passed,
elements at the beginning of the array are returned as long as the callback
returns truthy. The callback is invoked with three arguments:
(value, index, array). If a property name is passed for callback, the
created "_.pluck" style callback will return the property value of the
given element. If an object is passed for callback, the created "_.where"
style callback will return true for elements that have the properties of
the given object, else false.
:param list array: list to select from
:param mixed callback: callback to filter array
:rtype: mixed
"""
# reverse array, call first(), and reverse again
lst = first(array[::-1], callback)
if isinstance(lst, list):
lst = lst[::-1]
return lst
def last_index_of(array, value, from_index=0):
"""Gets the index at which the first occurrence of value is found
:param list array: list to search
:param mixed value: value to search for
:param integer from_index: the index to search from
:rtype: integer
"""
# reverse array, call index_of(), and subtract from max index
return len(array) - 1 - index_of(array[::-1], value, from_index)
def pull(array, *values):
"""Removes all provided values from the given array using strict equality for
comparisons, i.e. ===.
:param list array: list to modify
:param *values: values to remove
:rtype: list
"""
for value in values:
while array.count(value) > 0:
array.remove(value)
return array
def range_(*args, **kargs):
"""Creates a list of numbers (positive and/or negative) progressing from
start up to but not including end. If start is less than stop a zero-length
range is created unless a negative step is specified.
"""
return list(_range(*args, **kargs))
def remove(array, callback=None):
"""Removes all elements from a list that the callback returns truthy for
and returns an array of removed elements.
"""
removed = []
for is_true, _, i, _ in _iter_callback(array, callback):
if is_true:
removed.append(array.pop(i))
return removed
def rest(array, callback=None):
"""Return all but the first value of array. If a number n is passed, the
first n values are excluded from the result. If a callback function is
passed, elements at the beginning of the array are excluded from the result
as long as the callback returns truthy. The callback is invoked with three
arguments: (value, index, array). If a property name is passed for
callback, the created "_.pluck" style callback will return the property
value of the given element. If an object is passed for callback, the
created "_.where" style callback will return true for elements that have
the properties of the given object, else false.
:param list array: the list to process
:param mixed callback: callback to filter by
:rtype: list
"""
n = 0
for is_true, _, _, _ in _iter_callback(array, callback):
if is_true:
n += 1
else:
break
return array[n:]
tail = rest
drop = rest
def sorted_index(array, value, callback=None):
"""Determine the smallest index at which the value should be inserted into
array in order to maintain the sort order of the sorted array. If callback
is passed, it will be executed for value and each element in array to
compute their sort ranking. The callback is invoked with one argument:
(value). If a property name is passed for callback, the created "_.pluck"
style callback will return the property value of the given element. If an
object is passed for callback, the created "_.where" style callback will
return true for elements that have the properties of the given object, else
false.
:param list array: list to inspect
:param mixed value: value to evaluate
:param mixed callback: callback to determine sort key
:rtype: integer
"""
if callback:
# generate array of sorted keys computed using callback
cbk = _make_callback(callback)
array = sorted(cbk(item) for item in array)
value = cbk(value)
return bisect_left(array, value)
def union(*arrays):
"""Computes the union of the passed-in arrays using strict equality for
comparisons, i.e. ===.
:param list *arrays: lists to unionize
:rtype: list
"""
return uniq(flatten(arrays))
def uniq(array, callback=None):
"""Creates a duplicate-value-free version of the array using strict
equality for comparisons, i.e. ===. If callback is passed, each element of
array is passed through a callback before uniqueness is computed. The
callback is invoked with three arguments: (value, index, array). If a
property name is passed for callback, the created "_.pluck" style callback
will return the property value of the given element. If an object is passed
for callback, the created "_.where" style callback will return true for
elements that have the properties of the given object, else false.
:param list array: list to process
:param mixed callback: callback to filter array
:rtype: list
"""
if isinstance(callback, dict):
# FIXME: Resolve circular imports
from .collections import where
# where style callback; filter list using where
array = where(array, callback)
callback = None
if callback:
cbk = _make_callback(callback)
computed = [cbk(item) for item in array]
else:
computed = array
try:
# Try faster version of uniqifier which requires all array elements to
# be hashable.
lst = [array[i] for i, _ in _iter_unique_set(computed)]
except Exception: # pylint: disable=broad-except
# Fallback to version which doesn't require hashable elements but is
# slower.
lst = [array[i] for i, _ in _iter_unique(computed)]
return lst
unique = uniq
def without(array, *values):
"""Creates an array with all occurrences of the passed values removed using
strict equality for comparisons, i.e. ===.
:param list array: list to filter
:param mixed *values: values to remove
:rtype: list
"""
return [a for a in array if a not in values]
def xor(array, *lists):
"""Creates a list that is the symmetric difference of the provided lists.
"""
return (list(xor(set(array).symmetric_difference(lists[0]),
*lists[1:])) if lists
else array)
def zip_(*arrays):
"""Groups the elements of each array at their corresponding indexes.
Useful for separate data sources that are coordinated through matching
array indexes.
:param list *arrays: lists to process
:rtype: list
"""
# zip returns as a list of tuples so convert to list of lists
return [list(item) for item in zip(*arrays)]
def unzip(array):
"""The inverse of :func:`zipper`, this method splits groups of
elements into lists composed of elements from each group at their
corresponding indexes.
:param list array: list to process
:rtype: list
"""
return zip_(*array)
def zip_object(keys, values=None):
"""Creates a dict composed from lists of keys and values. Pass either a
single two dimensional list, i.e. [[key1, value1], [key2, value2]], or two
lists, one of keys and one of corresponding values.
:param list keys: either a list of keys or a list of [key, value] pairs
:param list values: list of values
:rtype: dict
"""
if values is None:
zipped = keys
else:
zipped = zip(keys, values)
return dict(zipped)
object_ = zip_object