Python List sort()方法

Python List sort() method sorts the list elements in the natural ordering. The sorting happens in-place, so the list is modified.

Python List sort()方法以自然顺序对列表元素进行排序。 排序发生在原位,因此列表被修改。

Python has a built-in function – sorted() – which is used to create a sorted list from an iterable.

Python具有内置函数sorted() ,该函数用于从可迭代对象创建排序列表。

1. Python排序列表(自然顺序) (1. Python Sort List in Natural Order)

When we sort a list of numbers, the natural ordering is to sort them in the increasing order.

当我们对数字列表进行排序时,自然的排序是按照升序对它们进行排序。

numbers_list = [3.4, 5.1, 2.2, 4.1, 1.0, 3.8]

print(f'Before sorting: {numbers_list}')

numbers_list.sort()

print(f'After sorting: {numbers_list}')

Output:

输出

Before sorting: [3.4, 5.1, 2.2, 4.1, 1.0, 3.8]
After sorting: [1.0, 2.2, 3.4, 3.8, 4.1, 5.1]
Recommended Read: 推荐阅读 : Python f-strings Python f字符串

The default sorting is implemented for strings also. Here is a simple example to sort a list of strings.

默认排序也针对字符串实现。 这是一个对字符串列表进行排序的简单示例。

str_list = ['a

你可能感兴趣的:(列表,python,java,编程语言,javascript,ViewUI)