-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpletons.py.bak
executable file
·78 lines (57 loc) · 1.96 KB
/
simpletons.py.bak
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
'''
Created on February 24, 2017
@author: optas
'''
import operator
import numpy as np
def sort_dict_by_key(in_dict, reverse=False):
return sorted(in_dict.items(), key=operator.itemgetter(0), reverse=reverse)
def sort_dict_by_val(in_dict, reverse=False):
return sorted(in_dict.items(), key=operator.itemgetter(1), reverse=reverse)
def invert_dictionary(d):
inv_map = {v: k for k, v in d.iteritems()}
return inv_map
def merge_two_dicts(x, y):
z = x.copy() # Start with x's keys and values.
z.update(y) # Modifies z with y's keys and values & returns None.
return z
def iterate_in_chunks(l, n):
'''Yield successive 'n'-sized chunks from iterable 'l'.
Note: last chunk will be smaller than l if n doesn't divide l perfectly.
'''
for i in xrange(0, len(l), n):
yield l[i:i + n]
def select_first_last_and_k(in_list, k):
'''Select the first and last element of a list among exactly k elements equally spaced
in the in_list[1:-1]
'''
f = in_list[0]
e = in_list[-1]
index = np.floor(np.linspace(1, len(in_list) - 2, k)).astype(np.int16)
res = [in_list[i] for i in index]
res.insert(0, f)
res.append(e)
return res
def indices_in_iterable(target, queries):
'''Find index of each item of the 'queries' in the 'target'.
If a query does not exist in the target, the corresponding index is set to -1.
'''
if len(np.unique(np.array(target, dtype=object))) != len(target):
raise ValueError('Target has to be comprised by unique elements.')
d = {name: i for i, name in enumerate(target)}
mapping = []
for name in queries:
if name not in d:
mapping.append(-1)
else:
mapping.append(d[name])
mapping = np.array(mapping)
return np.array(mapping)
def are_disjoint_sets(sets):
union = set()
for s in sets:
for x in s:
if x in union:
return False
union.add(x)
return True