Python Forum
translating lambda in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
translating lambda in function
#1
hello everyone,

i am a python beginner and would like to understand a bit more about lambda, how to "read" it and when to use it instead of functions. I am working with a list of points (x,y,z) and using this block of code to sort the list based on the z value:

orglst.sort(key=lambda orglst:(orglst.Z,orglst.X,orglst.Y))
now, this code works well. It was written by someone else and I cant really understand it because I am not familiar with lambda. I then tried to replace the lambda with a function, so I go a way I am familiar with, but it didnt work. In my mind, I was writing exactly the same instructions:

def reorder(list):
    return: list.Z,list.X,list.Y

orglst.sort(key=reorder(orglst))
then I tried something else, that also went wrong...

newlist = []
for point in orglst:
    newlist.append(point.Z)

sort.orglst(key=newlist)
Question 1: why are my alternatives not working?
Question 2: why would I use lambda instead of a function anyway?

Cheers!
Reply
#2
Your attempts failed mostly because you don't understand what the key function does.

The original sort is somewhat confusing because of the way it was written. I will rewrite here and hopefully things will be clearer
orglst.sort(key=lambda element:(element.Z,element.X,element.Y))
key is a function that is evaluated to get a sorting value. If the sort function wants to know how to order orglist[5] and orglist[9], it passes each to the key function and compares the results. In this example the key function creates a tuple(Z, X, Y). Sort knows how to compare tuples. First Z's are compared, then X and eventually Y if necessary.

You could do this with a function:
def sort_key(element):
    return (element.Z,element.X,element.Y)
 
orglst.sort(key=sort_key)
Your problem was that you were evaluating a function and passing that as the key. The result of the function is not a function, so sort complains.

Lambdas are commonly used as sorting keys because sorting keys are usually throw away functions, only used for doing the sort. Making them a regular function makes the key function look like it does something outside the scope of the sort. In most cases a regular function is a better idea because there is some overhead associated with lambdas.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 927 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Writing a lambda function that sorts dictionary GJG 1 2,030 Mar-09-2021, 06:44 PM
Last Post: buran
  Lambda function recursion error DeadlySocks 1 2,071 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  Lambda function error localsystemuser 3 4,747 Jan-29-2020, 01:43 PM
Last Post: DeaD_EyE
  Help with AWS Lambda function faws2019 0 1,589 Aug-27-2019, 01:54 PM
Last Post: faws2019
  Lambda function Uchikago 3 2,719 Jul-16-2019, 08:56 AM
Last Post: perfringo
  eval lambda function with restricted context Olivier 7 5,162 Mar-04-2019, 10:45 PM
Last Post: Olivier
  Using a lambda function within another function JChapman 8 5,438 Jan-08-2019, 01:54 PM
Last Post: JChapman
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,251 Apr-19-2018, 05:56 AM
Last Post: anandmn85
  Translating this to a list comprehension? gblomqvist 6 6,022 Dec-19-2016, 11:49 AM
Last Post: gblomqvist

Forum Jump:

User Panel Messages

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