How can one append dictionaries that each looping generates to a list so at the end my function returns a list of n-dictionaries? Without any fancy libraries, just plain python?
JavaScript
x
30
30
1
def inner_loop(*args):
2
for X, y in zip(X, y):
3
a_dict = {'t':None, 'p':None}
4
t_range = range(1, 10)
5
p_range = range('high','low')
6
best_acc = 0
7
list_of_dicts = []
8
9
#best grid selection
10
for t in t_range:
11
for p in p_range:
12
model(X, temp=t, press=p)
13
predict = model.prdeicting(y)
14
acc = (np.sum(predict == y))/len(y)
15
16
if acc > best_acc:
17
best_acc = acc
18
a_dict['t']= t
19
elif p == 'high':
20
a_dict.['p']='high'
21
elif p == 'low':
22
a_dict.['p']='low'
23
24
# I tried all options I knew here nothing really works:
25
list_of_dicts.append(a_dict)
26
list_of_dicts.append(a_dict.copy())
27
list_of_dicts.append(dict(a_dict))
28
29
return list_of_dicts
30
the function returns a instances of dictionaries but never an appended list of all dictionaries that were generated. Can somebody help with this?
Advertisement
Answer
JavaScript
1
9
1
def func(*args):
2
list_of_dicts = []
3
4
for X, y in zip(X, y):
5
a_dict = make_your_a_dict() # what you do in your example
6
list_of_dicts.append(a_dict)
7
8
return list_of_dicts # check indentation level
9
maybe it works.