Home  >  Article  >  Backend Development  >  Introduction to Python functions: functions and examples of sorted function

Introduction to Python functions: functions and examples of sorted function

WBOY
WBOYOriginal
2023-11-03 14:47:08985browse

Introduction to Python functions: functions and examples of sorted function

Introduction to Python functions: functions and examples of sorted functions

Python is a very powerful programming language with a wealth of built-in functions and modules. In this series of articles, we will introduce the commonly used functions of Python one by one and provide corresponding examples to help readers better understand and apply these functions. This article will introduce the functions and examples of the sorted function in detail.

sorted function is used to sort iterable objects and return a new sorted list. It can be used to sort various data types such as numbers, strings, lists, and tuples. The basic syntax of the sorted function is as follows:

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

where iterable represents the iterable object to be sorted, and key is an optional parameter. Use For specifying the basis for sorting, the default is None, that is, the elements are sorted according to themselves. reverse is an optional parameter that controls the ascending or descending order of the sorting results. The default is False, which means they are arranged in ascending order.

The following are some specific examples of sorted functions:

  1. Sort the list of numbers:

numbers = [6, 9, 3, 1, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)

Output result: [1, 3, 5, 6, 9]

  1. for string The list is sorted:

fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits)
print(sorted_fruits)

Output results: ['apple', 'banana', 'cherry', 'durian']

  1. Sort by string length:

fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits, key=len)
print(sorted_fruits)

Output result: ['apple ', 'cherry', 'banana', 'durian']

  1. Sort in the reverse order of the strings:

fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits, key=lambda x: x[::-1])
print(sorted_fruits)

Output result: ['banana', 'durian', 'cherry', 'apple']

  1. Sort the dictionary list according to the specified key:

students = [{'name ': 'Alice', 'age': 20}, {'name': 'Bob', 'age': 18}, {'name': 'Cathy', 'age': 22}]
sorted_students = sorted(students, key=lambda x: x['age'])
print(sorted_students)

Output result: [{'name': 'Bob', 'age': 18}, { 'name': 'Alice', 'age': 20}, {'name': 'Cathy', 'age': 22}]

The above example demonstrates the common usage of the sorted function. The sorted function has some other uses and parameters. The results can be sorted in descending order by setting the reverse parameter to True:

numbers = [6, 9, 3, 1, 5]
sorted_numbers = sorted(numbers, reverse=True)
print( sorted_numbers)

Output result: [9, 6, 5, 3, 1]

It should be noted that the sorted function will return a new sorted list and will not modify the original Iterable object. If you need to modify the original object, you can use the sort method of the list.

Summary:

This article introduces the functions and examples of the sorted function in detail. The sorted function is an important function for sorting in Python. It can sort various data types and supports custom sorting rules through the key parameter. By understanding the use of the sorted function, readers can better utilize Python for data processing and sorting operations.

I hope this article can help readers understand and use the sorted function. In subsequent articles, we will continue to introduce the functions and examples of other Python functions, so stay tuned.

The above is the detailed content of Introduction to Python functions: functions and examples of sorted function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn