码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [面试热点]深挖那些隐藏在sort和sorted背后的知识!


    锲子

    在Python开发的过程中,我们经常使用到.sort()和sorted()方法,但你知道这两者使用的差别么?

    在文章的开始,我来提出几个问题考考大家,看看大家对下面6个问题是否都清楚、明白。

    1. sorted()可以排序字符串么 ? 比如 sorted(‘adbc’)

    2. .sort()方法是否可以作用于集合与字典?

    3. arr = [('a', 1), ('b', 5), ('a', 3), ('b', 4)] 如何按照列表元素的字符升序和数字降序进行排列?

    4. 什么叫做 stable 稳定排序?

    5. 对上述arr列表,以下四种排序的结果一样么?

      arr = [('a', 1), ('b', 4), ('a', 2), ('b', 3)]
      # 第一种
      print(sorted(arr, key=lambda x: [x[0], -x[1]]))
      # 第二种
      print(sorted(sorted(arr, key=lambda x: x[0]), key=lambda x: -x[1]))
      # 第三种
      arr.sort(key=lambda x: [x[0], -x[1]])
      print(arr)
      # 第四种
      arr.sort(key=lambda x: x[0])
      arr.sort(key=lambda x: -x[1])
      print(arr)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    6. 你是否了解 cmp_to_key 方法的使用 ?

    sort和sorted的介绍

    今天我来带大家,深入了解 .sort()和sorted()方法,有哪些不为人知的细节!

    首先,我们来看看Python代码中,这两个方法的注释和所处的位置。

    以下内容,来自Python源码:

    # sort
    class list(object):   
        def sort(self, *args, **kwargs): # real signature unknown
            """
            Sort the list in ascending order and return None.
            
            The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
            order of two equal elements is maintained).
            
            If a key function is given, apply it once to each list item and sort them,
            ascending or descending, according to their function values.
            
            The reverse flag can be set to sort in descending order.
            """
            pass
        
    # sorted
    def sorted(*args, **kwargs): # real signature unknown
        """
        Return a new list containing all items from the iterable in ascending order.
        
        A custom key function can be supplied to customize the sort order, and the
        reverse flag can be set to request the result in descending order.
        """
        pass
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    通过源码注释以及日常使用的经验,可以总结一下表格

  • 相关阅读:
    【数据结构与算法】通过双向链表和HashMap实现LRU缓存 详解
    关于Redis在windows上运行及fork函数问题
    深度学习 --- stanford cs231学习笔记五(训练神经网络的几个重要组成部分之二,数据的预处理)
    Python潮流周刊#10:Twitter 的强敌 Threads 是用 Python 开发的!
    海南海口大型钢结构件3D扫描全尺寸三维测量平面度平行度检测-CASAIM中科广电
    【生活】浅浅记录
    Selenium获取本地已打开的浏览器页面进行跟踪和自定义日志记录
    k8s 通过helm发布应用
    RIP协议(路由信息协议)
    CEF 桌面软件开发实战
  • 原文地址:https://blog.csdn.net/BreezePython/article/details/126716192
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号