The records are stored in the database in this form:
| clu | name | a | b |
|---|---|---|---|
| q | one | 7 | 6 |
| q | two | 6 | 9 |
| q | five | 6 | 10 |
| e | three | 8 | 7 |
| e | four | 10 | 4 |
I am writing a function that will calculate the sum of the given values for different clu variables (for example, q and e). That is, first it is determined which values of name correspond to the value of e (these are three and four). Then there is an appeal to the dictionary "e_dict", which stores certain values corresponding to different names. The function should determine that for group e it is necessary to add the values of the "name" keys three and four and give the result.
Example of a dictionary e_dict:
{'one': {'u_mean': 4.25, 'c_mean': 4.25}, 'three': {'u_mean': 4.5, 'c_mean': 4.5}, 'two': {'u_mean': 4.583333333333334, 'c_mean': 4.583333333333334}, 'four': {'u_mean': 4.5625, 'c_mean': 4.5625}, 'five': {'u_mean': 4.65, 'c_mean': 4.65}}
The result should be like this:
{'e': {'u_mean': 4.531, 'c_mean': 4.531}, 'q': {'u_mean': 4.49443, 'c_mean': 4.49443}}
that is, the fields are all u_mean and their average is found, and they are also added.
The full code of my function:
def group_names():
st, c_clus, n_names = [], [], []
for h in Utilizations.objects.values('clu', 'name', 'a', 'b'):
st.append((h.get('clu'), h.get('name'), h.get('a'), h.get('b')))
c_clus.append(h.get('clu'))
n_names.append(h.get('name'))
"""получение названий"""
names, clus = [], []
for nam in n_names:
if nam not in names:
names.append(nam)
for cl in c_clus:
if cl not in clus:
clus.append(cl)
clu, e = {}, {}
u_load, u_max = {}, {}
mean_all, u_load_mean, u_max_mean = 0, 0, 0
for nam in names:
hs = Utilizations.objects.filter(name=nam)
o, p = 0, 0
for h in hs:
o += h.a
p += h.b
u_load[nam] = o / 2 + 1
u_max[nam] = p / 2 + 1
u_max_mean = mean(u_max.values())
u_load_mean = mean(u_load.values())
mean_all = (u_max_mean + u_load_mean) / 2
e[nam] = {'u_mean': mean_all, 'c_mean': mean_all}
for cl in clus:
for nam in names:
s = Utilizations.objects.filter(name=nam, clu=cl)
for h in hs:
clu[nam] = cl
return clu
It turns out to group in this form: {'one': 'q', 'two': 'q', 'five': 'q', 'three': 'e', 'four': 'e'}
And I don't know what to do next(