Please tell me the role of key=lambda of sorted

Asked 2 years ago, Updated 2 years ago, 16 views

Here, the second line of

sorted(data, key=lambda x: x['price'], reverse=true) [:limit]

I don't know why key=rambda x:x['price'] came out in the . (Please let me know what role Lambda plays in this.)

And let me know what x means!!

python

2022-09-21 17:11

2 Answers

The sorted function enables users to implement their own evaluation function to sort the resulting values so that they can compare different cases.

The reason for using lambda is to implement a simple one-line function.

temp = lambda x: x# The purpose is the same as below. 
def temp(x):
    return x

The code above is simple. We're going to go down to the price.


2022-09-21 17:11

The answers are already from above... I like lambda. You may not be familiar with it at first, but it's a concept that you use a lot, so I recommend you to set the concept clearly.


2022-09-21 17:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.