Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the parameters of the python sorted () function

Shulou Source: shulou.com Published: 2022-05-31 17:43:48 09月21日 Update

This article mainly introduces the relevant knowledge of "how to use the parameters of the python sorted () function". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the parameters of the python sorted () function" can help you solve the problem.

Concept

1. Sort the data in the iterable object and return a new list. Specify that lambda expressions are required for key sorting.

Grammar

Sorted (iterable, key=None, reverse=False)

Parameters.

Iterable-iterable object.

Key-elements that are mainly used for comparison, with only one parameter, the specific parameter of the function is taken from the iterable object, specifying an element in the iterable object to sort.

Reverse-collation, reverse = True descending, reverse = False ascending (default).

2. Specify the reverse order through the parameter reverse=True, and specify the field to be used in sorting by the parameter key.

(1) specify the reverse order through the parameter reverse=True:

> numbers = (4,5,2,8,9,1,0) > > sorted (numbers, reverse=True) [9,8,5,4,2,1,0]

(2) specify the fields to be used for sorting through the parameter key:

> codes = [('Shanghai', '021'), ('Beijing', '010'), ('Chengdu', '028'), ('Guangzhou', '020')] > sorted (codes, key=lambda x: X [1]) [('Beijing', '010'), ('Guangzhou', '020'), ('Shanghai', '021'), ('Chengdu') '028')] this is the end of the introduction to "how to use the parameters of the python sorted () function". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Parameters sorting functions objects iterations knowledge reverse order elements fields industries Shanghai Beijing Guangzhou Chengdu different practical content ascending order only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Huawei macOS MariaDB