当前位置: 首页 > news >正文

067、Python 高阶函数的编写:优质冒泡排序

以下写了个简单的冒泡排序函数:

def bubble_sort(items: list) -> list:for i in range(1, len(items)):swapped = Falsefor j in range(0, len(items) - 1):if items[j] > items[j + 1]:items[j], items[j + 1] = items[j + 1], items[j]swapped = Trueif not swapped:breakif __name__ == '__main__':nums = [55, 66, 9, 22, 86, 35, 44, 97, 56]bubble_sort(nums)print(nums)  # 输出结果:[9, 22, 35, 44, 55, 56, 66, 86, 97]

上面写法虽然正确排好序了,但初始化变量nums的结果改变了,如果实际应用不需要把初始变量改变,该如何?

优化后:

def bubble_sort(items: list) -> list:items = items[:]  # 把列表数据赋给一个新变量并作为返回值for i in range(1, len(items)):swapped = Falsefor j in range(0, len(items) - 1):if items[j] > items[j + 1]:items[j], items[j + 1] = items[j + 1], items[j]swapped = Trueif not swapped:breakreturn itemsif __name__ == '__main__':nums = [55, 66, 9, 22, 86, 35, 44, 97, 56]print(bubble_sort(nums))  # 输出结果:[9, 22, 35, 44, 55, 56, 66, 86, 97]print(nums)  # 输出结果:[55, 66, 9, 22, 86, 35, 44, 97, 56]  原来值并没有改变

如此修改后初始化变量就可以保留,又可以输出排好序的数据了。

这点是基于以下编程思想:

在我们设计函数的时候,一定要注意函数的无副作用(调用函数不影响调用者)。优化后函数质量提升了。

但是该函数的功能还不够全面,假如我对于排序输出结果既要按升序输出,也要按降调输出,又该如何?

方法就是增加一个布尔值变量:

def bubble_sort(items: list, ascending=True) -> list:  # 增加一个bool变量items = items[:]  # 把列表数据赋给一个新变量并作为返回值for i in range(1, len(items)):swapped = Falsefor j in range(0, len(items) - 1):if items[j] > items[j + 1]:items[j], items[j + 1] = items[j + 1], items[j]swapped = Trueif not swapped:breakif not ascending:items = items[::-1]return itemsif __name__ == '__main__':nums = [55, 66, 9, 22, 86, 35, 44, 97, 56]print(bubble_sort(nums))  # 输出结果:[9, 22, 35, 44, 55, 56, 66, 86, 97]print(bubble_sort(nums, ascending=False))  # 输出结果:[97, 86, 66, 56, 55, 44, 35, 22, 9]

如此,我们就可以通过变量ascending的值来判断按升序还是降序输出结果。

但是优化后的函数还不够好,因为在if items[j] > items[j + 1]:语句存在一定的耦合性。那么又该如何解耦呢?

方法就通过引入函数变量:

def bubble_sort(items: list, ascending: bool = True, gt=lambda x, y: x > y) -> list:  # 增加一个bool变量,并引入一个Lambda函数items = items[:]  # 把列表数据赋给一个新变量并作为返回值for i in range(1, len(items)):swapped = Falsefor j in range(0, len(items) - 1):if gt(items[j], items[j + 1]):  # 通过调用函数做大小比较items[j], items[j + 1] = items[j + 1], items[j]swapped = Trueif not swapped:breakif not ascending:items = items[::-1]return itemsif __name__ == '__main__':nums = [55, 66, 9, 22, 86, 35, 44, 97, 56]print(bubble_sort(nums))  # 输出结果:[9, 22, 35, 44, 55, 56, 66, 86, 97]print(bubble_sort(nums, ascending=False))  # 输出结果:[97, 86, 66, 56, 55, 44, 35, 22, 9]

如此优化后,该函数质量就很高了,功能更全面,灵活性更高。

为什么这么说,看以下应用:


def bubble_sort(items: list, ascending: bool = True, gt=lambda x, y: x > y) -> list:  # 增加一个bool变量,并引入一个Lambda函数"""冒泡排序:param items: 待排序的列表:param ascending:是否使用升序:param gt: 比较两个元素大小的函数:return: 返回排序后列表"""items = items[:]  # 把列表数据赋给一个新变量并作为返回值for i in range(1, len(items)):swapped = Falsefor j in range(0, len(items) - 1):if gt(items[j], items[j + 1]):  # 通过调用函数做大小比较items[j], items[j + 1] = items[j + 1], items[j]swapped = Trueif not swapped:breakif not ascending:items = items[::-1]return itemsif __name__ == '__main__':nums = [55, 66, 9, 22, 86, 35, 44, 97, 56]print(bubble_sort(nums))  # 输出结果:[9, 22, 35, 44, 55, 56, 66, 86, 97]print(bubble_sort(nums, ascending=False))  # 输出结果:[97, 86, 66, 56, 55, 44, 35, 22, 9]words = ['Apple', 'Banana', 'Orange', 'Strawberry', 'Grape', 'Watermelon']print(bubble_sort(words, gt=lambda x, y: len(x) > len(y), ascending=False))# 输出结果 ['Watermelon', 'Strawberry', 'Orange', 'Banana', 'Grape', 'Apple']

如上,当一个列表数字是字符串,我需要把输出结果按字符串长度进行排序输出,那么只需要在调用函数的时候,修改函数变量的函数就可以实现了。

这就是高阶函数!

http://www.lryc.cn/news/379273.html

相关文章:

  • 【Python】从基础到进阶(一):了解Python语言基础以及变量的相关知识
  • AI学习指南机器学习篇-KNN的优缺点
  • 全网最全!25届最近5年上海理工大学自动化考研院校分析
  • LANG、LC_MESSAGES和LC_ALL
  • 生成式AI和LLM的一些基本概念和名词解释
  • python项目(课设)——飞机大战小游戏项目源码(pygame)
  • Chatgpt教我打游戏攻略
  • 最全信息收集工具集
  • redis类型解析汇总
  • Unity3d自定义TCP消息替代UNet实现网络连接
  • git fetch 和 git pull区别
  • 冲击2024年CSDN博客之星TOP1:CSDN文章质量分查询在哪里?
  • 高性能并行计算华为云实验一:MPI矩阵运算
  • 库卡机器人减速机维修齿轮磨损故障
  • 【C/C++】我自己提出的数组探针的概念,快来围观吧
  • ArcGIS图斑分区(组)排序—从上到下从左到右
  • React useRef 组件内及组件传参使用
  • Intelij IDEA中Mapper.xml无法构建到资源目录的问题
  • 2024.6.23周报
  • 鸿蒙实战开发:网络层的艺术——优雅封装与搭建指南(中)
  • docker in docker 连私有仓库时报错 https
  • mac怎么压缩pdf文件,苹果电脑怎么压缩pdf文件大小
  • 兴顺物流管理系统的设计
  • 力扣(2024.06.21)
  • 飞机大战java
  • Springboot的自动配置原理
  • Interview preparation--elascitSearch深分页问题
  • C语言笔试题:实现把一个无符号整型数字的二进制序列反序后输出
  • elementplus如何实现dialog遮罩层外的元素可以被操作点击
  • Springboot整合Kafka消息队列服务实例