Python Forum
How to define a function to create a resorted list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to define a function to create a resorted list?
#1
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.

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.
Reply
#2
You only need to return the list instead of printing it
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()
    return [a[b[i][1]] for i in range(len(a))]

...
result = sortedleastdev(L, mean(L))
Functions that return values are much more useful than functions that print values.
Reply
#3
Thanks a lot for the help, I feel progress!
Reply
#4
Why not use a sorting key?
Reply
#5
(Aug-07-2020, 05:13 PM)deanhystad Wrote: Why not use a sorting key?

How does that work? Further info would be appreciated.
Reply
#6
Look up sorting keys in the python documentation. They even provide examples. I wrote a 3 line script to sort you list by deviation from the mean. Used the standard sorted() call and used the key to convert values in the list to a deviation value.

https://docs.python.org/3/howto/sorting.html
Reply
#7
(Aug-07-2020, 05:26 PM)deanhystad Wrote: Look up sorting keys in the python documentation. They even provide examples. I wrote a 3 line script to sort you list by deviation from the mean. Used the standard sorted() call and used the key to convert values in the list to a deviation value.

https://docs.python.org/3/howto/sorting.html

Great to learn about that. It does simplify a lot! Thanks very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,362 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 807 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 757 Nov-09-2022, 01:50 PM
Last Post: korenron
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Create a function for writing to SQL data to csv mg24 4 1,111 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 905 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  how to easily create a list of already existing item CompleteNewb 15 3,382 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Create SQLite columns from a list or tuple? snakes 6 8,518 May-04-2021, 12:06 PM
Last Post: snakes
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,595 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020