详解Python3迁移接口变化采坑记

作者:qinjianhuang 时间:2022-12-25 14:29:49 

1、除法相关

在python3之前,


print 13/4  #result=3

然而在这之后,却变了!


print(13 / 4) #result=3.25

"/”符号运算后是正常的运算结果,那么,我们要想只取整数部分怎么办呢?原来在python3之后,“//”有这个功能:


print(13 // 4) #result=3.25

是不是感到很奇怪呢?下面我们再来看一组结果:


print(4 / 13)   # result=0.3076923076923077
print(4.0 / 13)  # result=0.3076923076923077
print(4 // 13)  # result=0
print(4.0 // 13) # result=0.0
print(13 / 4)   # result=3.25
print(13.0 / 4)  # result=3.25
print(13 // 4)  # result=3
print(13.0 // 4) # result=3.0

2、Sort()和Sorted()函数中cmp参数发生了变化(重要)

在python3之前:


def reverse_numeric(x, y):
 return y - x
print sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)

输出的结果是:[5, 4, 3, 2, 1]

但是在python3中,如果继续使用上面代码,则会报如下错误:

TypeError: 'cmp' is an invalid keyword argument for this function

咦?根据报错,意思是在这个函数中cmp不是一个合法的参数?为什么呢?查阅文档才发现,在python3中,需要把cmp转化为一个key才可以:


def cmp_to_key(mycmp):
 'Convert a cmp= function into a key= function'
 class K:
   def __init__(self, obj, *args):
     self.obj = obj
   def __lt__(self, other):
     return mycmp(self.obj, other.obj) < 0
   def __gt__(self, other):
     return mycmp(self.obj, other.obj) > 0
   def __eq__(self, other):
     return mycmp(self.obj, other.obj) == 0
   def __le__(self, other):
     return mycmp(self.obj, other.obj) <= 0
   def __ge__(self, other):
     return mycmp(self.obj, other.obj) >= 0
   def __ne__(self, other):
     return mycmp(self.obj, other.obj) != 0
 return K

为此,我们需要把代码改成:


from functools import cmp_to_key

def comp_two(x, y):
 return y - x

numList = [5, 2, 4, 1, 3]
numList.sort(key=cmp_to_key(comp_two))
print(numList)

这样才能输出结果!

具体可参考链接:Sorting HOW TO

3、map()函数返回值发生了变化

Python 2.x 返回列表,Python 3.x 返回迭代器。要想返回列表,需要进行类型转换!


def square(x):
 return x ** 2

map_result = map(square, [1, 2, 3, 4])
print(map_result)    # <map object at 0x000001E553CDC1D0>
print(list(map_result)) # [1, 4, 9, 16]

# 使用 lambda 匿名函数
print(map(lambda x: x ** 2, [1, 2, 3, 4]))  # <map object at 0x000001E553CDC1D0>

来源:https://blog.csdn.net/sinat_35512245/article/details/79618263

标签:Python3,迁移接口
0
投稿

猜你喜欢

  • Webpack中的文件指纹的实现

    2024-04-10 11:00:17
  • Python实现栈和队列的简单操作方法示例

    2023-09-28 19:07:51
  • 解决nohup执行python程序log文件写入不及时的问题

    2021-06-16 14:04:30
  • javascript面向对象三大特征之封装实例详解

    2023-08-23 21:39:04
  • Python 抓取动态网页内容方案详解

    2022-05-19 02:09:29
  • IE6与IE7的unshift 方法

    2010-01-19 13:59:00
  • 理解Javascript的caller,callee,call,apply区别

    2024-04-17 10:00:50
  • Laravel框架实现发送短信验证功能代码

    2024-05-03 15:29:08
  • 2018年Python值得关注的开源库、工具和开发者(总结篇)

    2023-01-18 08:32:15
  • 浅谈MySQL8.0 异步复制的三种方式

    2024-01-29 00:26:46
  • Python中DataFrame判断两列数据是否相等的方法

    2023-12-09 10:17:37
  • Pandas按周/月/年统计数据介绍

    2022-01-15 18:49:14
  • MySQL如何通过Navicat实现远程连接

    2024-01-13 23:07:07
  • 利用Python写个摸鱼监控进程

    2022-11-04 10:40:45
  • SQL2005中char nchar varchar nvarchar数据类型的区别和使用环境讲解

    2024-01-25 23:10:46
  • 如何让新安装的MySQL数据库变得更安全

    2009-01-04 13:19:00
  • 调试Django时打印SQL语句的日志代码实例

    2021-06-09 05:29:42
  • Python连接MySQL并使用fetchall()方法过滤特殊字符

    2024-01-23 07:35:36
  • golang 如何通过反射创建新对象

    2024-04-27 15:24:38
  • python 实现简单的FTP程序

    2021-03-29 10:33:00
  • asp之家 网络编程 m.aspxhome.com