Home > Article > Backend Development > 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:
numbers = [6, 9, 3, 1, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits)
print(sorted_fruits)
fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits, key=len)
print(sorted_fruits)
fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits, key=lambda x: x[::-1])
print(sorted_fruits)
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)
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)
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!