Python 中使用 Lambda 函数进行自定义排序的指南

PythonBeginner
立即练习

介绍

Python 的 lambda 函数提供了一种简洁而强大的方式来执行自定义排序操作。在本教程中,我们将探讨如何使用 lambda 函数在 Python 中对数据进行排序,从而为数据操作和分析解锁新的可能性。到最后,你将对利用 lambda 函数进行自定义排序有一个扎实的理解,并能够将这些技术应用于你自己的 Python 项目。

理解 Lambda 函数

在 Python 中,lambda 函数是一个使用 lambda 关键字定义的小型匿名函数。与使用 def 关键字的常规函数不同,lambda 函数可以内联创建,并且不需要单独的定义。

让我们创建一个新的 Python 文件来探索 lambda 函数。打开 WebIDE 并在 /home/labex/project/lambda_sorting 目录中创建一个名为 basic_lambda.py 的新文件。

将以下代码添加到 basic_lambda.py

## Define a regular function
def square_function(x):
    return x * x

## The equivalent lambda function
square_lambda = lambda x: x * x

## Test both functions
number = 5
print(f"Using regular function: {number}² = {square_function(number)}")
print(f"Using lambda function: {number}² = {square_lambda(number)}")

## Lambda with multiple arguments
add = lambda x, y: x + y
print(f"{3} + {4} = {add(3, 4)}")

现在让我们在终端中运行这个文件:

python3 basic_lambda.py

你应该看到这样的输出:

Using regular function: 5² = 25
Using lambda function: 5² = 25
3 + 4 = 7

当你需要一个简单的函数来使用一小段时间时,lambda 函数特别有用,尤其是在使用 map()filter()sorted() 等函数时。让我们创建另一个示例,看看 lambda 函数与 map() 函数一起使用的情况。

创建一个名为 lambda_with_map.py 的新文件,内容如下:

## Using lambda with map() to apply a function to all items in a list
numbers = [1, 2, 3, 4, 5]

## Square all numbers using map with a lambda function
squared_numbers = list(map(lambda x: x * x, numbers))

print(f"Original numbers: {numbers}")
print(f"Squared numbers: {squared_numbers}")

## Double all numbers using map with a lambda function
doubled_numbers = list(map(lambda x: x * 2, numbers))
print(f"Doubled numbers: {doubled_numbers}")

运行这个文件:

python3 lambda_with_map.py

你应该看到:

Original numbers: [1, 2, 3, 4, 5]
Squared numbers: [1, 4, 9, 16, 25]
Doubled numbers: [2, 4, 6, 8, 10]

在我们将 lambda 函数应用于排序之前,理解 lambda 函数至关重要。在下一步中,我们将探讨如何将 lambda 函数与 Python 的排序函数一起使用。

使用 Lambda 函数进行基本排序

Python 提供了两种主要的排序集合的方法:sorted() 函数和列表的 .sort() 方法。我们可以将 lambda 函数与两者结合使用,以创建自定义排序逻辑。

让我们创建一个名为 basic_sorting.py 的新文件,以探索使用 lambda 函数进行基本排序:

## Basic sorting examples
fruits = ["apple", "banana", "cherry", "date", "elderberry", "fig"]

## Sort alphabetically (default behavior)
alphabetical = sorted(fruits)
print(f"Alphabetical order: {alphabetical}")

## Sort by length using a lambda function
by_length = sorted(fruits, key=lambda x: len(x))
print(f"Sorted by length: {by_length}")

## Sort by the last character
by_last_char = sorted(fruits, key=lambda x: x[-1])
print(f"Sorted by last character: {by_last_char}")

## Sort in reverse order (longest to shortest)
longest_first = sorted(fruits, key=lambda x: len(x), reverse=True)
print(f"Longest to shortest: {longest_first}")

运行这个文件:

python3 basic_sorting.py

你应该看到类似这样的输出:

Alphabetical order: ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']
Sorted by length: ['fig', 'date', 'apple', 'cherry', 'banana', 'elderberry']
Sorted by last character: ['banana', 'apple', 'date', 'fig', 'cherry', 'elderberry']
Longest to shortest: ['elderberry', 'banana', 'cherry', 'apple', 'date', 'fig']

sorted() 中的 key 参数接受一个函数,该函数在比较之前转换每个项目。Lambda 函数提供了一种简洁的方式来创建这些转换。

现在让我们尝试使用更复杂的数据结构进行排序。创建一个名为 sorting_tuples.py 的文件:

## Sorting lists of tuples
students = [
    ("Alice", 21, 85),
    ("Bob", 19, 90),
    ("Charlie", 22, 78),
    ("David", 20, 92),
    ("Eve", 18, 88)
]

print("Original student data:")
for student in students:
    print(f"  {student[0]}: age {student[1]}, grade {student[2]}")

## Sort by name (first element in tuple)
by_name = sorted(students, key=lambda student: student[0])
print("\nSorted by name:")
for student in by_name:
    print(f"  {student[0]}: age {student[1]}, grade {student[2]}")

## Sort by age (second element)
by_age = sorted(students, key=lambda student: student[1])
print("\nSorted by age:")
for student in by_age:
    print(f"  {student[0]}: age {student[1]}, grade {student[2]}")

## Sort by grade (third element)
by_grade = sorted(students, key=lambda student: student[2], reverse=True)
print("\nSorted by grade (highest first):")
for student in by_grade:
    print(f"  {student[0]}: age {student[1]}, grade {student[2]}")

运行这个文件:

python3 sorting_tuples.py

你应该看到学生以不同的方式排序:

Original student data:
  Alice: age 21, grade 85
  Bob: age 19, grade 90
  Charlie: age 22, grade 78
  David: age 20, grade 92
  Eve: age 18, grade 88

Sorted by name:
  Alice: age 21, grade 85
  Bob: age 19, grade 90
  Charlie: age 22, grade 78
  David: age 20, grade 92
  Eve: age 18, grade 88

Sorted by age:
  Eve: age 18, grade 88
  Bob: age 19, grade 90
  David: age 20, grade 92
  Alice: age 21, grade 85
  Charlie: age 22, grade 78

Sorted by grade (highest first):
  David: age 20, grade 92
  Bob: age 19, grade 90
  Eve: age 18, grade 88
  Alice: age 21, grade 85
  Charlie: age 22, grade 78

在下一步中,我们将探索使用 lambda 函数的更高级的排序技术。

高级排序技术

现在我们了解了基础知识,让我们探索使用 lambda 函数的更高级的排序技术。

排序字典

字典是 Python 中常见的数据结构,我们经常需要对字典列表进行排序。创建一个名为 sorting_dictionaries.py 的文件:

## Sorting lists of dictionaries
products = [
    {"name": "Laptop", "price": 999.99, "stock": 25},
    {"name": "Phone", "price": 499.50, "stock": 42},
    {"name": "Tablet", "price": 299.75, "stock": 15},
    {"name": "Headphones", "price": 149.99, "stock": 34},
    {"name": "Mouse", "price": 24.99, "stock": 55}
]

## Print original products
print("Original product list:")
for product in products:
    print(f"  {product['name']}: ${product['price']}, Stock: {product['stock']}")

## Sort by price (lowest to highest)
by_price = sorted(products, key=lambda product: product["price"])
print("\nSorted by price (lowest to highest):")
for product in by_price:
    print(f"  {product['name']}: ${product['price']}, Stock: {product['stock']}")

## Sort by stock (highest to lowest)
by_stock = sorted(products, key=lambda product: product["stock"], reverse=True)
print("\nSorted by stock (highest to lowest):")
for product in by_stock:
    print(f"  {product['name']}: ${product['price']}, Stock: {product['stock']}")

## Sort by name (alphabetically)
by_name = sorted(products, key=lambda product: product["name"])
print("\nSorted by name:")
for product in by_name:
    print(f"  {product['name']}: ${product['price']}, Stock: {product['stock']}")

运行这个文件:

python3 sorting_dictionaries.py

你应该看到产品以不同的方式排序:

Original product list:
  Laptop: $999.99, Stock: 25
  Phone: $499.5, Stock: 42
  Tablet: $299.75, Stock: 15
  Headphones: $149.99, Stock: 34
  Mouse: $24.99, Stock: 55

Sorted by price (lowest to highest):
  Mouse: $24.99, Stock: 55
  Headphones: $149.99, Stock: 34
  Tablet: $299.75, Stock: 15
  Phone: $499.5, Stock: 42
  Laptop: $999.99, Stock: 25

Sorted by stock (highest to lowest):
  Mouse: $24.99, Stock: 55
  Phone: $499.5, Stock: 42
  Headphones: $149.99, Stock: 34
  Laptop: $999.99, Stock: 25
  Tablet: $299.75, Stock: 15

Sorted by name:
  Headphones: $149.99, Stock: 34
  Laptop: $999.99, Stock: 25
  Mouse: $24.99, Stock: 55
  Phone: $499.5, Stock: 42
  Tablet: $299.75, Stock: 15

多级排序

有时我们需要使用多个标准对项目进行排序。例如,我们可能希望首先按年级对学生进行排序,然后对年级相同的学生按年龄排序。

创建一个名为 multi_level_sorting.py 的文件:

## Multi-level sorting example
students = [
    {"name": "Alice", "grade": "A", "age": 21},
    {"name": "Bob", "grade": "B", "age": 19},
    {"name": "Charlie", "grade": "A", "age": 22},
    {"name": "David", "grade": "C", "age": 20},
    {"name": "Eve", "grade": "B", "age": 18},
    {"name": "Frank", "grade": "A", "age": 19}
]

print("Original student list:")
for student in students:
    print(f"  {student['name']}: Grade {student['grade']}, Age {student['age']}")

## Sort by grade (A to C), then by age (youngest to oldest)
## To sort by grade, we create a dictionary for grade priorities
grade_priority = {"A": 1, "B": 2, "C": 3}  ## A is highest priority (lowest number)

by_grade_then_age = sorted(students,
                           key=lambda student: (grade_priority[student["grade"]], student["age"]))

print("\nSorted by grade (A first), then by age (youngest first):")
for student in by_grade_then_age:
    print(f"  {student['name']}: Grade {student['grade']}, Age {student['age']}")

## Sort by age (oldest to youngest), then by name (alphabetically)
by_age_then_name = sorted(students,
                          key=lambda student: (-student["age"], student["name"]))

print("\nSorted by age (oldest first), then by name:")
for student in by_age_then_name:
    print(f"  {student['name']}: Grade {student['grade']}, Age {student['age']}")

运行这个文件:

python3 multi_level_sorting.py

你应该看到:

Original student list:
  Alice: Grade A, Age 21
  Bob: Grade B, Age 19
  Charlie: Grade A, Age 22
  David: Grade C, Age 20
  Eve: Grade B, Age 18
  Frank: Grade A, Age 19

Sorted by grade (A first), then by age (youngest first):
  Frank: Grade A, Age 19
  Alice: Grade A, Age 21
  Charlie: Grade A, Age 22
  Eve: Grade B, Age 18
  Bob: Grade B, Age 19
  David: Grade C, Age 20

Sorted by age (oldest first), then by name:
  Charlie: Grade A, Age 22
  Alice: Grade A, Age 21
  David: Grade C, Age 20
  Bob: Grade B, Age 19
  Frank: Grade A, Age 19
  Eve: Grade B, Age 18

注意我们如何在 lambda 函数中使用元组来定义多个排序标准。元组中的第一个元素是主要的排序键,后续元素用于决胜局。

我们使用负号 -student["age"] 来按降序对年龄进行排序。当你想按数值逆序排序时,这种技术很有用。

排序自定义对象

让我们创建一个最终示例来展示如何对自定义类对象进行排序。创建一个名为 sorting_objects.py 的文件:

## Sorting objects of a custom class
class Person:
    def __init__(self, name, age, height):
        self.name = name
        self.age = age
        self.height = height  ## in cm

    def __repr__(self):
        return f"Person(name='{self.name}', age={self.age}, height={self.height}cm)"

## Create a list of Person objects
people = [
    Person("Alice", 25, 165),
    Person("Bob", 30, 180),
    Person("Charlie", 22, 175),
    Person("David", 35, 170),
    Person("Eve", 28, 160)
]

print("Original list of people:")
for person in people:
    print(f"  {person}")

## Sort by age
by_age = sorted(people, key=lambda person: person.age)
print("\nSorted by age:")
for person in by_age:
    print(f"  {person}")

## Sort by height
by_height = sorted(people, key=lambda person: person.height)
print("\nSorted by height:")
for person in by_height:
    print(f"  {person}")

## Sort by name length, then by age
by_name_length_then_age = sorted(people, key=lambda person: (len(person.name), person.age))
print("\nSorted by name length, then by age:")
for person in by_name_length_then_age:
    print(f"  {person}")

运行这个文件:

python3 sorting_objects.py

你应该看到:

Original list of people:
  Person(name='Alice', age=25, height=165cm)
  Person(name='Bob', age=30, height=180cm)
  Person(name='Charlie', age=22, height=175cm)
  Person(name='David', age=35, height=170cm)
  Person(name='Eve', age=28, height=160cm)

Sorted by age:
  Person(name='Charlie', age=22, height=175cm)
  Person(name='Alice', age=25, height=165cm)
  Person(name='Eve', age=28, height=160cm)
  Person(name='Bob', age=30, height=180cm)
  Person(name='David', age=35, height=170cm)

Sorted by height:
  Person(name='Eve', age=28, height=160cm)
  Person(name='Alice', age=25, height=165cm)
  Person(name='David', age=35, height=170cm)
  Person(name='Charlie', age=22, height=175cm)
  Person(name='Bob', age=30, height=180cm)

Sorted by name length, then by age:
  Person(name='Bob', age=30, height=180cm)
  Person(name='Eve', age=28, height=160cm)
  Person(name='Alice', age=25, height=165cm)
  Person(name='David', age=35, height=170cm)
  Person(name='Charlie', age=22, height=175cm)

这个例子演示了如何使用 lambda 函数通过直接访问自定义对象的属性来对它们进行排序。

实际应用中的排序

现在让我们将我们所学到的应用于更现实的数据分析场景。我们将创建一个程序,该程序分析一个书籍数据集,并允许按不同的标准进行排序。

创建一个名为 book_analyzer.py 的文件:

## Book data analysis application
books = [
    {
        "title": "The Great Gatsby",
        "author": "F. Scott Fitzgerald",
        "year": 1925,
        "pages": 180,
        "rating": 4.2,
        "genres": ["Classic", "Fiction"]
    },
    {
        "title": "To Kill a Mockingbird",
        "author": "Harper Lee",
        "year": 1960,
        "pages": 281,
        "rating": 4.3,
        "genres": ["Classic", "Fiction", "Drama"]
    },
    {
        "title": "1984",
        "author": "George Orwell",
        "year": 1949,
        "pages": 328,
        "rating": 4.2,
        "genres": ["Dystopian", "Fiction", "Political"]
    },
    {
        "title": "The Hobbit",
        "author": "J.R.R. Tolkien",
        "year": 1937,
        "pages": 310,
        "rating": 4.4,
        "genres": ["Fantasy", "Adventure"]
    },
    {
        "title": "Harry Potter and the Sorcerer's Stone",
        "author": "J.K. Rowling",
        "year": 1997,
        "pages": 309,
        "rating": 4.5,
        "genres": ["Fantasy", "Adventure", "Young Adult"]
    },
    {
        "title": "The Catcher in the Rye",
        "author": "J.D. Salinger",
        "year": 1951,
        "pages": 214,
        "rating": 3.8,
        "genres": ["Fiction", "Coming of Age"]
    }
]

def print_books(book_list, heading):
    """Helper function to print books in a formatted way"""
    print(f"\n{heading}")
    print("-" * 80)
    for book in book_list:
        genres = ", ".join(book["genres"])
        print(f"{book['title']} by {book['author']} ({book['year']}) - {book['pages']} pages")
        print(f"  Rating: {book['rating']}, Genres: {genres}")
    print("-" * 80)

## Print the original list
print_books(books, "Original Book List")

## Sort books by different criteria
sort_options = {
    "1": ("Title (A-Z)", lambda b: b["title"]),
    "2": ("Author (A-Z)", lambda b: b["author"]),
    "3": ("Publication Year (Oldest First)", lambda b: b["year"]),
    "4": ("Publication Year (Newest First)", lambda b: -b["year"]),
    "5": ("Rating (Highest First)", lambda b: -b["rating"]),
    "6": ("Number of Pages (Lowest First)", lambda b: b["pages"]),
    "7": ("Number of Genres", lambda b: len(b["genres"])),
}

## Show sorting options
print("\nSorting Options:")
for key, (description, _) in sort_options.items():
    print(f"{key}. {description}")

## Automatically show all sorting examples for this tutorial
for option, (description, sort_key) in sort_options.items():
    sorted_books = sorted(books, key=sort_key)
    print_books(sorted_books, f"Books Sorted by {description}")

运行这个文件:

python3 book_analyzer.py

你将看到书籍以多种方式排序:

Original Book List
--------------------------------------------------------------------------------
The Great Gatsby by F. Scott Fitzgerald (1925) - 180 pages
  Rating: 4.2, Genres: Classic, Fiction
To Kill a Mockingbird by Harper Lee (1960) - 281 pages
  Rating: 4.3, Genres: Classic, Fiction, Drama
1984 by George Orwell (1949) - 328 pages
  Rating: 4.2, Genres: Dystopian, Fiction, Political
The Hobbit by J.R.R. Tolkien (1937) - 310 pages
  Rating: 4.4, Genres: Fantasy, Adventure
Harry Potter and the Sorcerer's Stone by J.K. Rowling (1997) - 309 pages
  Rating: 4.5, Genres: Fantasy, Adventure, Young Adult
The Catcher in the Rye by J.D. Salinger (1951) - 214 pages
  Rating: 3.8, Genres: Fiction, Coming of Age
--------------------------------------------------------------------------------

Sorting Options:
1. Title (A-Z)
2. Author (A-Z)
3. Publication Year (Oldest First)
4. Publication Year (Newest First)
5. Rating (Highest First)
6. Number of Pages (Lowest First)
7. Number of Genres

Books Sorted by Title (A-Z)
...

[Output continues with all the different sorting examples]

这个例子演示了如何在实际应用中使用 lambda 函数来提供灵活的排序功能。注意我们如何将 lambda 函数及其描述存储在字典中,这使得在实际应用中添加更多排序选项变得容易。

让我们创建一个交互式版本,用户可以选择一个排序选项。创建一个名为 interactive_book_sorter.py 的文件:

## Interactive book sorter application
books = [
    {
        "title": "The Great Gatsby",
        "author": "F. Scott Fitzgerald",
        "year": 1925,
        "pages": 180,
        "rating": 4.2,
        "genres": ["Classic", "Fiction"]
    },
    {
        "title": "To Kill a Mockingbird",
        "author": "Harper Lee",
        "year": 1960,
        "pages": 281,
        "rating": 4.3,
        "genres": ["Classic", "Fiction", "Drama"]
    },
    {
        "title": "1984",
        "author": "George Orwell",
        "year": 1949,
        "pages": 328,
        "rating": 4.2,
        "genres": ["Dystopian", "Fiction", "Political"]
    },
    {
        "title": "The Hobbit",
        "author": "J.R.R. Tolkien",
        "year": 1937,
        "pages": 310,
        "rating": 4.4,
        "genres": ["Fantasy", "Adventure"]
    },
    {
        "title": "Harry Potter and the Sorcerer's Stone",
        "author": "J.K. Rowling",
        "year": 1997,
        "pages": 309,
        "rating": 4.5,
        "genres": ["Fantasy", "Adventure", "Young Adult"]
    },
    {
        "title": "The Catcher in the Rye",
        "author": "J.D. Salinger",
        "year": 1951,
        "pages": 214,
        "rating": 3.8,
        "genres": ["Fiction", "Coming of Age"]
    }
]

def print_books(book_list, heading):
    """Helper function to print books in a formatted way"""
    print(f"\n{heading}")
    print("-" * 80)
    for book in book_list:
        genres = ", ".join(book["genres"])
        print(f"{book['title']} by {book['author']} ({book['year']}) - {book['pages']} pages")
        print(f"  Rating: {book['rating']}, Genres: {genres}")
    print("-" * 80)

## Define our sorting options
sort_options = {
    "1": ("Title (A-Z)", lambda b: b["title"]),
    "2": ("Author (A-Z)", lambda b: b["author"]),
    "3": ("Publication Year (Oldest First)", lambda b: b["year"]),
    "4": ("Publication Year (Newest First)", lambda b: -b["year"]),
    "5": ("Rating (Highest First)", lambda b: -b["rating"]),
    "6": ("Number of Pages (Lowest First)", lambda b: b["pages"]),
    "7": ("Number of Genres", lambda b: len(b["genres"])),
}

## Print the original list
print_books(books, "Book Catalog")

while True:
    ## Show sorting options
    print("\nHow would you like to sort the books?")
    for key, (description, _) in sort_options.items():
        print(f"{key}. {description}")
    print("0. Exit")

    choice = input("\nEnter your choice (0-7): ")

    if choice == "0":
        print("Thank you for using the Book Sorter. Goodbye!")
        break
    elif choice in sort_options:
        description, sort_key = sort_options[choice]
        sorted_books = sorted(books, key=sort_key)
        print_books(sorted_books, f"Books Sorted by {description}")
    else:
        print("Invalid choice. Please try again.")

运行这个交互式版本:

python3 interactive_book_sorter.py

这将允许你选择不同的排序选项并查看结果。完成后,输入 "0" 退出程序。

这个交互式应用程序演示了 lambda 函数如何使在实际应用中实现各种排序选项变得容易。每个排序选项都只是一个 lambda 函数,它从书籍字典中提取相关信息。

总结

祝贺你完成了关于在 Python 中使用 lambda 函数进行自定义排序的教程。你已经学到了:

  1. lambda 函数的基础知识以及它们与常规函数的区别
  2. 如何将 lambda 函数与 Python 的排序函数(sorted().sort())一起使用
  3. 对不同数据结构进行排序的技术:
    • 简单值的列表
    • 元组的列表
    • 字典的列表
    • 自定义对象
  4. 使用 lambda 函数中的元组进行多级排序
  5. 如何在实际的数据分析应用中应用 lambda 函数

这些技能将在你未来的 Python 项目中对数据处理、分析和操作非常有帮助。Lambda 函数提供了一种简洁而强大的方式来定制排序行为,使你的代码更具表现力,并且更易于维护。

在你继续你的 Python 之旅时,你会发现 lambda 函数不仅对排序有用,而且对其他高阶函数(如 map()filter()reduce())也很有用。你在本教程中学到的技术将为这些更高级的应用奠定坚实的基础。