Analysis of key in sorted function

Source: Internet
Author: User

In Python programming, it is often important to sort the data in various ways. But when we use the sorted built-in function, we find that it has three parameters.

The first parameter is an iterative data, which can be a string, an array, a list, and so on.

The third one is whether the output is inverted.

So what's the second one? By looking at the text on the image, you can find that key is a function that iterates the order of data by defining the sort.

By looking at the data, there are probably two main ways to implement the key= function:

1, anonymous function

With this example, you can see that word is the value of each iteration, that is, from strawbe iteration to raspberry. On the right side of the lambda, the value is reversed, for example strawberry into yrrbewarts.

2, General function

This also enables the same functionality as anonymous functions.

Analysis of key in sorted function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.