forked from mrvgao/Python-advanced-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
itertools_show.py
88 lines (59 loc) · 1.77 KB
/
itertools_show.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
"""" Itertools
"""
import itertools
from dataclasses import dataclass
import random
some_names = ['GRU', 'CNN', 'LSTM']
lr = [1e-3, 1e-4, 3e-5, 1e-6]
gamma = [1e-3, 1e-2, 1e-1]
# set{A} X set{B}
@dataclass
class Model:
lr: float
gamma: float
class CNN(Model):
pass
class GRU(Model):
pass
class LSTM(Model):
pass
def run_a_model(model_name, lr, gamma):
model_name_mapping = {
'CNN': CNN,
'LSTM': LSTM,
'GRU': GRU,
}
print(f'running model {model_name} as {lr} {gamma} ')
acc = model_name_mapping[model_name](lr, gamma)
print(acc)
# group by
if __name__ == '__main__':
# for p in itertools.product(some_names, lr, gamma):
# run_a_model(*p)
for n in itertools.permutations(some_names, r=2):
print(n)
print('*'*8)
for n in itertools.combinations(some_names, r=2):
print(n)
mock_login = ['uid22111', 'uid2213', 'uid2322', 'uid2344', 'uid2321'] * 20
random.shuffle(mock_login)
print(mock_login)
for g, elements in itertools.groupby(mock_login, key=lambda n: int(n[-1]) % 2):
print(g, list(elements))
numbers = [2, 1, 31, 21, 41, 1, 31, 12, 1, 43, 1, 43, 12, 43, 11, 12, 31]
max_list = [2, 2, 31, 31, 41, 41, 41, 41]
# for 0 -> N
print(list(itertools.accumulate(numbers, max)))
print(list(itertools.accumulate(numbers, min)))
print(list(itertools.accumulate(numbers, lambda x, y: x + y)))
# for r in itertools.tee(numbers, 3):
# print(list(r))
lines = open('itertools_show.py')
# for lines_copy in itertools.tee(lines, 2):
# for line in lines: # -> Pytorch, tensorflow: Dataloader
# print(line)
for line in lines:
print(line)
print('#'*8)
for line in lines:
print(line)