Aug-06-2020, 06:33 PM
After day-long trial-and-error and research I found the code from web which does what I tried to do, i.e. resorting a list according to absolute difference to a value.
I'm sure it's a very newbie question. I tried to search through the web without progress though. Much appreciated.
def sortleastdev(a, val):
b =[[0 for x in range(2)] for y in range(len(a))]
for i in range(0, len(a)):
b[i][0] = abs(a[i]-val)
b[i][1] = i
b.sort()
for i in range(0, len(a)):
print(a[b[i][1]])
a = [7, 12, 2, 4, 8, 0]
val = 6
sortleastdev(a, val)
from statistics import mean
L = [1.238623532,1.315924461,1.430787909,0.65436604,0.78646411,1.551692625,1.143410924,1.044302349,1.12971696,1.007285185,1.009553518,0.646888596,1.027950548,0.950471257,1.048221271,1.070840989]
sortleastdev(L, mean(L))But what if I wish to define "sortleastdev" as a new list? How to make it giving me a new resorted list instead, where "sortleastdev(L, mean(L))[0]" would be 1.070840989?I'm sure it's a very newbie question. I tried to search through the web without progress though. Much appreciated.
