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


Messages In This Thread
translating lambda in function - by fabs - Apr-28-2020, 04:30 AM
RE: translating lambda in function - by deanhystad - Apr-28-2020, 05:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 953 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Writing a lambda function that sorts dictionary GJG 1 2,046 Mar-09-2021, 06:44 PM
Last Post: buran
  Lambda function recursion error DeadlySocks 1 2,095 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  Lambda function error localsystemuser 3 4,779 Jan-29-2020, 01:43 PM
Last Post: DeaD_EyE
  Help with AWS Lambda function faws2019 0 1,606 Aug-27-2019, 01:54 PM
Last Post: faws2019
  Lambda function Uchikago 3 2,742 Jul-16-2019, 08:56 AM
Last Post: perfringo
  eval lambda function with restricted context Olivier 7 5,218 Mar-04-2019, 10:45 PM
Last Post: Olivier
  Using a lambda function within another function JChapman 8 5,483 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,276 Apr-19-2018, 05:56 AM
Last Post: anandmn85
  Translating this to a list comprehension? gblomqvist 6 6,079 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