如何检查 Python 中集合转换为列表后是否已排序

PythonPythonBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何判断一个集合在 Python 中转换为列表后是否为有序列表。本实验将探讨如何从列表进行集合转换以去除重复项,并展示如何使用 list() 函数将集合转换回列表。

然后,你将学习如何通过将结果列表与其排序后的版本进行比较来检查该列表是否有序。这包括创建 Python 脚本,将列表转换为集合、打印结果集合、将集合转换回列表,最后实现一种方法来验证列表是否有序。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/sets("Sets") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") subgraph Lab Skills python/lists -.-> lab-559565{{"如何检查 Python 中集合转换为列表后是否已排序"}} python/sets -.-> lab-559565{{"如何检查 Python 中集合转换为列表后是否已排序"}} python/build_in_functions -.-> lab-559565{{"如何检查 Python 中集合转换为列表后是否已排序"}} python/data_collections -.-> lab-559565{{"如何检查 Python 中集合转换为列表后是否已排序"}} end

探索集合转换

在这一步中,你将学习如何在 Python 中将列表转换为集合。集合(Set)是无序的唯一元素集合。这意味着集合不能包含重复的值。将列表转换为集合是一种从列表中去除重复元素的有效方法。

首先,使用 VS Code 编辑器在你的 ~/project 目录下创建一个名为 convert_to_set.py 的 Python 文件。

## Create a list with duplicate elements
my_list = [1, 2, 2, 3, 4, 4, 5]

## Convert the list to a set
my_set = set(my_list)

## Print the set
print(my_set)

现在,让我们运行这个 Python 脚本。打开你的终端并导航到 ~/project 目录:

cd ~/project

然后,使用 python 命令执行脚本:

python convert_to_set.py

你应该会看到以下输出:

{1, 2, 3, 4, 5}

如你所见,重复的元素已被去除,集合中仅包含唯一的值。

让我们尝试另一个使用字符串的示例:

## Create a list of strings with duplicates
string_list = ["apple", "banana", "apple", "orange", "banana"]

## Convert the list to a set
string_set = set(string_list)

## Print the set
print(string_set)

保存对 convert_to_set.py 所做的更改并再次运行脚本:

python convert_to_set.py

输出将是:

{'orange', 'banana', 'apple'}

请注意,集合中元素的顺序可能与原始列表不同,因为集合是无序的。

转换为列表并检查排序

在这一步中,你将学习如何将集合转换回列表,然后检查该列表是否已排序。这需要使用 list() 函数将集合转换为列表,然后将排序后的列表与原始列表进行比较。

让我们继续使用 ~/project 目录下的 convert_to_set.py 文件。我们将添加代码,把集合转换回列表,然后对其进行排序。

## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

## Convert the list to a set to remove duplicates
my_set = set(my_list)

## Convert the set back to a list
my_list_from_set = list(my_set)

## Print the list obtained from the set
print("List from set:", my_list_from_set)

## Sort the list
my_list_from_set.sort()

## Print the sorted list
print("Sorted list:", my_list_from_set)

现在,让我们运行这个 Python 脚本。打开你的终端并导航到 ~/project 目录:

cd ~/project

然后,使用 python 命令执行脚本:

python convert_to_set.py

你应该会看到类似于以下的输出:

List from set: [1, 2, 3, 4, 5, 6, 9]
Sorted list: [1, 2, 3, 4, 5, 6, 9]

第一行显示了从集合创建的列表,该列表包含唯一元素,但可能不是原始顺序。第二行显示了排序后的列表。

现在,让我们添加一个检查,看看原始列表(去除重复项并转换为列表后)是否已排序。

## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

## Convert the list to a set to remove duplicates
my_set = set(my_list)

## Convert the set back to a list
my_list_from_set = list(my_set)

## Sort the list
sorted_list = sorted(my_list_from_set)

## Check if the list is sorted
if my_list_from_set == sorted_list:
    print("The list is already sorted.")
else:
    print("The list is not sorted.")

## Print the sorted list
print("Sorted list:", sorted_list)

再次运行脚本:

python convert_to_set.py

你应该会看到类似于以下的输出:

The list is not sorted.
Sorted list: [1, 2, 3, 4, 5, 6, 9]

这表明从集合中获取的列表最初并未排序,并且使用了 sorted() 函数来创建一个排序后的版本。

使用 sorted() 进行比较

在这一步中,你将学习如何使用 sorted() 函数进行比较,同时不修改原始列表。sorted() 函数会从可迭代对象返回一个新的排序列表,而 .sort() 方法则是对列表进行原地排序。当你想保留原始列表不变时,这会很有用。

让我们继续使用 ~/project 目录下的 convert_to_set.py 文件。我们将修改代码以使用 sorted() 函数,并将其与原始列表进行比较。

## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

## Convert the list to a set to remove duplicates
my_set = set(my_list)

## Convert the set back to a list
my_list_from_set = list(my_set)

## Use sorted() to get a new sorted list
sorted_list = sorted(my_list_from_set)

## Print the original list from set
print("Original list from set:", my_list_from_set)

## Print the sorted list
print("Sorted list:", sorted_list)

## Check if the original list is equal to the sorted list
if my_list_from_set == sorted_list:
    print("The original list is sorted.")
else:
    print("The original list is not sorted.")

现在,让我们运行这个 Python 脚本。打开你的终端并导航到 ~/project 目录:

cd ~/project

然后,使用 python 命令执行脚本:

python convert_to_set.py

你应该会看到类似于以下的输出:

Original list from set: [1, 2, 3, 4, 5, 6, 9]
Sorted list: [1, 2, 3, 4, 5, 6, 9]
The original list is sorted.

在这种情况下,从集合中获取的原始列表恰好已经是排好序的。让我们修改原始列表,以确保它最初是未排序的。

## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

## Convert the list to a set to remove duplicates
my_set = set(my_list)

## Convert the set back to a list
my_list_from_set = list(my_set)

## Shuffle the list to make sure it's not sorted
import random
random.shuffle(my_list_from_set)

## Use sorted() to get a new sorted list
sorted_list = sorted(my_list_from_set)

## Print the original list from set
print("Original list from set:", my_list_from_set)

## Print the sorted list
print("Sorted list:", sorted_list)

## Check if the original list is equal to the sorted list
if my_list_from_set == sorted_list:
    print("The original list is sorted.")
else:
    print("The original list is not sorted.")

再次运行脚本:

python convert_to_set.py

你应该会看到类似于以下的输出:

Original list from set: [9, 2, 4, 5, 6, 1, 3]
Sorted list: [1, 2, 3, 4, 5, 6, 9]
The original list is not sorted.

现在,原始列表被打乱了顺序,sorted() 函数提供了一个排序后的版本用于比较,这表明原始列表保持不变。

总结

在本次实验中,你学习了如何在 Python 中将列表转换为集合,从而有效地去除重复元素。集合是无序的数据集合,因此在转换过程中元素的顺序可能会发生变化。

你还开始学习如何将集合转换回列表,并检查得到的列表是否已排序。这需要使用 list() 函数,并将排序后的列表与原始列表进行比较。