MST

星途 面试题库

面试题:Python中sorted对列表排序的基础应用

假设有一个列表my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5],请使用Python的sorted函数对其进行升序排序,并将排序后的结果赋值给新列表sorted_list,然后打印sorted_list。另外,说明sorted函数返回的是什么类型的数据。
24.9万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = sorted(my_list)
print(sorted_list)

sorted函数返回的是一个列表(list)类型的数据。它会返回一个新的已排序列表,而原列表保持不变。