Home > Backend Development > Python Tutorial > How to use the sorted() function in Python to sort a sequence

How to use the sorted() function in Python to sort a sequence

王林
Release: 2023-08-22 09:30:20
Original
2087 people have browsed it

How to use the sorted() function in Python to sort a sequence

How to use the sorted() function to sort sequences in Python

Sort is a commonly used operation in data processing and analysis. In Python, you can sort a sequence using the sorted() function. The sorted() function is a built-in function that can sort sequences such as lists, tuples, and strings, and return a newly sorted sequence. This article will introduce how to use the sorted() function and provide code examples.

1. Basic usage of sorted() function

The basic syntax of sorted() function is as follows:

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

Among them, iterable represents the iterable object to be sorted, such as list, tuple, string, etc. The key parameter is optional and is used to specify the comparison rule when sorting. The reverse parameter is optional and defaults to False, which means sorting in ascending order; if set to True, it means sorting in descending order.

The following is a basic usage example of the sorted() function:

  1. Sort the list:

nums = [3, 1, 2, 4 , 5]
sorted_nums = sorted(nums)
print(sorted_nums) # Output: [1, 2, 3, 4, 5]

  1. Sort strings:

string = "hello world"
sorted_string = sorted(string)
print(sorted_string) # Output: [' ', 'd', 'e', ​​'h', 'l ', 'l', 'l', 'o', 'o', 'r', 'w']

  1. Sort tuples:

tuple = (3, 1, 2, 4, 5)
sorted_tuple = sorted(tuple)
print(sorted_tuple) # Output: [1, 2, 3, 4, 5]

二, In-depth understanding of the key parameter

The key parameter is used to specify the comparison rule when sorting. It can be a function or a Lambda expression. The function of the key parameter is to process each element and then sort according to the processing results.

The following is an example of using the key parameter:

  1. Sort the string list by string length:

names = ['Alice', 'Bob', 'Charlie', 'David']
sorted_names = sorted(names, key=len)
print(sorted_names) # Output: ['Bob', 'Alice', 'David', 'Charlie ']

  1. Sort the dictionary list by dictionary key value:

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

  1. Sort a list of strings in reverse alphabetical order:

words = ['apple', 'banana ', 'cherry', 'durian']
sorted_words = sorted(words, key=lambda x: x[::-1])
print(sorted_words) # Output: ['banana', 'cherry' , 'apple', 'durian']

3. Use the reverse parameter to implement descending sorting

The reverse parameter is used to specify the sorting order. The default is False, which means sorting in ascending order; if set to True, it means sorting in descending order.

The following is an example of using the reverse parameter:

numbers = [3, 1, 2, 4, 5]
sorted_numbers_asc = sorted(numbers)
sorted_numbers_desc = sorted(numbers , reverse=True)
print(sorted_numbers_asc) # Output: [1, 2, 3, 4, 5]
print(sorted_numbers_desc) # Output: [5, 4, 3, 2, 1]

4. Summary

This article introduces the basic usage of the sorted() function in Python and gives code examples. Through the sorted() function, we can easily sort sequences such as lists, tuples, and strings. The key parameter can be used to specify the sorting rule, and the reverse parameter can be used to specify the sorting order. Mastering the use of the sorted() function, we can process and analyze data more efficiently.

I hope this article will help you understand the use of the sorted() function. thanks for reading!

The above is the detailed content of How to use the sorted() function in Python to sort a sequence. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template