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 see how it works in detail.

如果您想学习如何在Python项目中使用sort()方法,那么本文适合您。 此方法功能非常强大,您可以根据需要对其进行自定义,因此让我们详细了解它的工作原理。

You will learn:

您将学习:

  • How to use this method and customize its functionality.

    如何使用此方法并自定义其功能。
  • When to use it and when not to use 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 based 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! ⭐

你准备好了吗? 让我们开始! ⭐

目的和用例 (Purpose and Use Cases)

With the sort() method, you can sort a list in either:

使用sort()方法,可以对以下任一列表进行排序:

  • Ascending Order

    升序
  • Descending Order

    降序

This method is used 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 promise!), but for now it's very important that you know that the sort() method modifies the list, so its original version is lost.

您将在本文中了解有关突变的更多信息(我保证!),但是现在很重要的一点是,您知道sort()方法会修改列表,因此它的原始版本会丢失。

Because of this, you should only use 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 see 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()方法的工作方式,可以传递两个可选参数:

  • Key

  • Reverse

    逆转

Let's see how they change the behavior of this method. Here we have a method call with these 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>

  • reverse=<value>

    reverse=<value>

This is because they are keyword-only arguments. 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:

这是因为它们是仅关键字的参数 。 如果要为它们传递自定义值,则必须在方法调用中指定它们的名称 ,后跟等号=及其对应的值,如下所示:

Otherwise, if you try to pass the arguments directly as we normally do for positional parameters, you will see this error because the function will not know which argument corresponds to which parameter:

否则,如果您尝试像通常对位置参数一样直接传递参数,则会看到此错误,因为函数将不知道哪个参数对应于哪个参数:

TypeError: sort() takes no positional arguments

逆转 (Reverse)

Now that you know what keyword-only arguments are, let's start wi

  • 10
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值