python升序和降序排序_Python排序列表数组⽅法–通过⽰例解
释升序和降序
python升序和降序排序
If you want to learn how to work with the sort() method in your Python projects, then this article is for you. This method is very powerful and you can customize it to fit your needs, so let's e how it works in detail.
如果您想学习如何在Python项⽬中使⽤sort()⽅法,那么本⽂适合您。 此⽅法功能⾮常强⼤,您可以根据需要对其进⾏⾃定义,因此让我们详细了解它的⼯作原理。
You will learn:
您将学习:
How to u this method and customize its functionality.
如何使⽤此⽅法并⾃定义其功能。
When to u it and when not to u it.
何时使⽤和何时不使⽤它。
How to call it passing different combinations of arguments.
如何通过不同的参数组合调⽤它。
济南的冬天教案
How to sort a list in ascending and descending order.
如何按升序和降序对列表进⾏排序。
How to compare the elements of a list bad on intermediate values.
如何基于中间值⽐较列表的元素。
How you can pass lambda functions to this method.
如何将lambda函数传递给此⽅法。
How this method compares to the sorted() function.
此⽅法与sorted()函数的⽐较。
Why the sort() method performs a stable sort.
为什么sort()⽅法执⾏稳定的排序。
How the process of mutation works behind the scenes.
突变过程如何在后台⼯作。
Are you ready? Let's begin!
你准备好了吗? 让我们开始!
⽬的和⽤例 (Purpo and U Cas)
With the sort() method, you can sort a list in either:
使⽤sort()⽅法,可以对以下任⼀列表进⾏排序:
Ascending Order
升序
Descending Order
This method is ud to sort a list in place, which means that it mutates it or modifies it directly without creating additional copies, so remember:
此⽅法⽤于对列表进⾏排序,这意味着它会对其进⾏突变或直接对其进⾏修改,⽽⽆需创建其他副本,因此请记住:
You will learn more about mutation in this article (I promi!), but for now it's very important that you know that the sort() method modifies the list, so its original version is lost.
您将在本⽂中了解有关突变的更多信息(我保证!),但是现在很重要的⼀点是,您知道sort()⽅法会修改列表,因此它的原始版本会丢失。
正在营业
Becau of this, you should only u this method if:
因此,仅在以下情况下才应使⽤此⽅法:
You want to modify (sort) the list permanently.
您要永久修改(排序)列表。
You don't need to keep the original version of the list.
您⽆需保留列表的原始版本。
If this fits your needs, then the .sort() method is exactly what you are looking for.
如果这符合您的需求,那么.sort()⽅法正是您想要的。
语法和参数 ( Syntax and Arguments)
Let's e how you can call .sort() to take advantage of its full power.
让我们看看如何调⽤.sort()来利⽤其全部功能。
This is the most basic call (with no arguments):
这是最基本的调⽤(⽆参数):
If you don't pass any arguments, by default:
如果您不传递任何参数,则默认情况下:
The list will be sorted in ascending order.
该列表将按升序排序。
The elements of the list will be compared directly using their values with the < operator.
列表中的元素将使⽤其值与<;运算符直接进⾏⽐较。
For example:
例如:
>>> b = [6, 3, 8, 2, 7, 3, 9]
>>> b.sort()
>>> b
[2, 3, 3, 6, 7, 8, 9] # Sorted!
⾃定义参数 (Custom Arguments )
To customize how the sort() method works, you can pass two optional arguments:
要⾃定义sort()⽅法的⼯作⽅式,可以传递两个可选参数:
键
Rever
逆转
Let's e how they change the behavior of this method. Here we have a method call with the two arguments:
让我们看看它们如何更改此⽅法的⾏为。 这⾥有两个参数的⽅法调⽤:
Before explaining how they work, I would like to explain something that you probably noticed in the diagram above – in the method call, the names of the parameters have to be included before their corresponding values, like this:
在解释它们如何⼯作之前,我想解释⼀下您可能在上图中注意到的⼀些内容–在⽅法调⽤中,必须在参数的相应值之前包括参数名称,如下所⽰:
key=<f>
key=<f>入党的要求
rever=<value>
rever=<value>
This is becau they are . If you are passing a custom value for them, their names have to be specified in the method call, followed by an equal sign = and their corresponding values, like this:
这是因为它们是 。 如果要为它们传递⾃定义值,则必须在⽅法调⽤中指定它们的名称 ,后跟等号=及其对应的值,如下所⽰:
Otherwi, if you try to pass the arguments directly as we normally do for positional parameters, you will e this error becau the function will not know which argument corresponds to which parameter:
高中英语自我介绍
否则,如果您尝试像通常对位置参数⼀样直接传递参数,则会看到此错误,因为函数将不知道哪个参数对应于哪个参数:
TypeError: sort() takes no positional arguments
逆转 (Rever)
Now that you know what keyword-only arguments are, let's start with rever.
现在您知道什么是仅关键字参数,让我们从rever开始。
The value of rever can be either True or Fal:
平常的意思rever的值可以为True或Fal :
Fal means that the list will be sorted in ascending order.
Fal表⽰列表将按升序排序。
True means that the list will be sorted in descending (rever) order.
True表⽰列表将以降序(反向)排序。
Tip: By default, its value is Fal – if you don't pass any arguments for this parameter, the list is sorted in ascending order.
提⽰:默认情况下,其值为Fal –如果您不为此参数传递任何参数,则列表将按升序排序。
Here we have a few examples:
这⾥有⼀些例⼦:
# List of Integers
>>> b = [6, 3, 8, 2, 7, 3, 9]
>>> b.sort()
>>> b
[2, 3, 3, 6, 7, 8, 9]
# List of Strings
>>> c = ["A", "Z", "D", "T", "U"]
>>> c.sort()
>>> c
['A', 'D', 'T', 'U', 'Z']
Tip: If the elements of the list are strings, they are sorted alphabetically.
提⽰:如果列表中的元素是字符串,则它们将按字母顺序排序。
# List of Integers
>>> b = [6, 3, 8, 2, 7, 3, 9]
>>> b.sort(rever=True)
>>> b
[9, 8, 7, 6, 3, 3, 2]
# List of Strings
>>> c = ["A", "Z", "D", "T", "U"]
>>> c.sort(rever=True)
>>> c
['Z', 'U', 'T', 'D', 'A']
Tip: Notice how the list is sorted in descending order if rever is True.
提⽰:请注意,如果rever为True则列表将按降序排序。
键 (Key)
Now that you know how to work with the rever parameter, let's e the key parameter.
现在您知道如何使⽤rever参数,让我们来看⼀下key参数。
This parameter is a little bit more detailed becau it determines how the elements of the list are be compared during the sorting process.
此参数稍微详细⼀点,因为它确定在排序过程中如何⽐较列表中的元素。
The value of key is either:
key的值是:
None, which means that the elements of the list will be compared directly. For example, in a list of integers, the integers themlves can be ud for the comparison.
None ,这意味着将直接⽐较列表中的元素。 例如,在整数列表中,整数本⾝可⽤于⽐较。
A function of one argument that generates an intermediate value for each element. This intermediate value is calculated
only once and it's ud to make the comparisons during the entire sorting process. We u this when we don't want to compare the elements directly, for example, when we want to compare strings bad on their length (the intermediate value).
⼀个参数的 函数 ,为每个元素⽣成⼀个中间值。 该中间值仅计算⼀次,并在整个排序过程中⽤于进⾏⽐较。 当我们不想直接⽐较元素时,例如当我们要根据字符串的长度(中间值)⽐较字符串时,可以使⽤它。
Tip: By default, the value of key is None, so the elements are compared directly.
心安胶囊
提⽰:默认情况下, key值为None ,因此可以直接⽐较元素。
For example:
例如:
Let's say that we want to sort a list of strings bad on their length, from the shortest string to the longest string. We can pass the function len as the value of key, like this:
假设我们要根据字符串的长度(从最短的字符串到最长的字符串)对字符串列表进⾏排序。 我们可以将函数len作为key的值传递,如下所⽰:
>>> d = ["aaa", "bb", "c"]
>>> d.sort(key=len)
>>> d恶心想吐是什么原因
['c', 'bb', 'aaa']
Tip: Notice that we are only passing the name of the function (len) without parenthesis becau we a
re not calling the function. This is very important.
提⽰:请注意,由于我们没有调⽤函数,因此仅传递函数名称( len )⽽没有括号。 这个⾮常重要。
Notice the difference between comparing the elements directly and comparing their length (e below). Using the default value of key (None) would have sorted the strings alphabetically (left), but now we are sorting them bad on their length (right):
注意直接⽐较元素和⽐较元素长度之间的区别(请参见下⽂)。 使⽤key的默认值( None )可以按字母顺序对字符串进⾏排序(左),但是现在我们根据字符串的长度(右)对其进⾏排序:
What happens behind the scenes? Each element is pasd as an argument to the len() function, and the value returned by this function call is ud to perform the comparisons during the sorting process:
幕后发⽣了什么? 每个元素都作为参数传递给len()函数,此函数调⽤返回的值⽤于在排序过程中执⾏⽐较:
This results in a list with a different sorting criteria: length.
这将导致列表具有不同的排序标准:长度。
Here we have another example:
这⾥有另⼀个例⼦:
Another interesting example is sorting a list of strings as if they were all written in lowerca letters (for example, making "Aa" equivalent to "aa").
另⼀个有趣的⽰例是对字符串列表进⾏排序,就好像它们都是⽤⼩写字母书写⼀样(例如,使“ Aa”等效于“ aa”)。
According to lexicographical order, capital letters come before lowerca letters:
根据字典顺序,⼤写字母先于⼩写字母:
>>> "E" < "e"
True
So the string "Emma" would come before "emily" in a sorted list, even if their lowerca versions would be in the opposite order:
晚会活动策划方案
因此,即使它们的⼩写版本的排列顺序相反,字符串"Emma"也将在排序列表中的"emily"之前出现: