-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
41 lines (33 loc) · 887 Bytes
/
util.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
def get_bin_index(x, start, end, bin):
assert(x <= end and x >= start)
return int(x*bin/(end-start))
def normalize(X):
if type(X) == type([]):
total = 0.0
for val in X:
total += val
for i in range(len(X)):
X[i] = X[i]/total
return X
if type(X) == type({}):
total = 0.0
for val in X.values():
total += val
for key in X:
X[key] = X[key]/total
return X
return None
def UnionDict(d1, d2):
dict = d1
for k, v in d2.items():
if k in dict:
if type(v) != type(""):
dict[k] = dict[k] + v
else:
dict[k] = v
else:
dict[k] = v
return dict
if __name__=="__main__":
#print normalize({1:1,2:2,3:4})
print get_bin_index(10, 0, 9, 5)