I'm trying to define a function which would give multiple value outputs after some tedious calculation. Based on my current knowledge the best I can do now is to let it return a dictionary. Below is a simplified version.
def external(a, b, lval, hval, yr):
G, E = [], []
for i in range(len(a)):
G.append(0 if min(b[i], hval) < max(a[i], lval) else min(b[i], hval) - max(a[i], lval))
for i in range(len(a)):
E.append(b[i] - a[i] - G[i])
return dict([('ratio', max(E[:-yr])), ('index', E.index(max(E[:-yr])))])Otherwise I will have to copy the function like the following as I don't know how to define multiple functions base on the same definition process, but this obviously would not be the best choice of any programmer.def externalratio(a, b, lval, hval, yr):
G, E = [], []
for i in range(len(a)):
G.append(0 if min(b[i], hval) < max(a[i], lval) else min(b[i], hval) - max(a[i], lval))
for i in range(len(a)):
E.append(b[i] - a[i] - G[i])
return max(E[:-yr]))
def externalindex(a, b, lval, hval, yr):
G, E = [], []
for i in range(len(a)):
G.append(0 if min(b[i], hval) < max(a[i], lval) else min(b[i], hval) - max(a[i], lval))
for i in range(len(a)):
E.append(b[i] - a[i] - G[i])
return E.index(max(E[:-yr]))However the dictionary way becomes problematic when I further write functions on top of that. First that's not very user-friendly and second it would return an error upon further calculation in a real example (to simplify the case that's not shown here), which strangely would not happen in the single definition way that doesn't involve dictionary return. def maxexternalratio(a, b, yr):
Etemp = []
for i in range(len(a)):
for j in range(len(a)):
Etemp.append(external(a, b, a[i], b[j], yr)['ratio'])
return max(Etemp)So how can I define multiple functions which are results of the same calculation process, like "external.ratio" and "external.index", without requiring to copy the same thing multiple times? Any help would be very appreciated.
