Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses Lambdas to sort

Shulou Source: shulou.com Published: 2022-06-01 05:26:52 10月01日 Update

This article is about how python uses Lambdas sorting. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Sort with Lambdas

Lambdas expressions are anonymous functions that can receive multiple parameters with a single-line expression. A common use of lambdas is to set it to the key parameter in the built-in sorted () function.

In addition, lambdas is often used in max (), map () and other functions. Among these functions, you can replace regular functions that use the def keyword with a single-line expression.

> students = [{'name':'John',' score': 98}, {'name':' Mike',' score': 94}, {'name':'Jennifer',' score': 99}] > sorted (students, key=lambda x: X ['score']) [{' name':' Mike', 'score': 94}, {' name':'John', 'score': 98}, {' name':'Jennifer'] 'score': 99}] Thank you for reading! This is the end of this article on "how python uses Lambdas sorting". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Function sort expression content parameter more article good practical key keyword multiple common regular article see knowledge other than that reference help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Xiaomi Docker macOS