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


Posted in Python onOctober 11, 2019

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
简单的Apache+FastCGI+Django配置指南
Jul 22 Python
使用Python神器对付12306变态验证码
Jan 05 Python
利用标准库fractions模块让Python支持分数类型的方法详解
Aug 11 Python
Python cookbook(数据结构与算法)保存最后N个元素的方法
Feb 13 Python
python Flask 装饰器顺序问题解决
Aug 08 Python
Python基础之文件读取的讲解
Feb 16 Python
详解python读取image
Apr 03 Python
python aiohttp的使用详解
Jun 20 Python
python脚本实现音频m4a格式转成MP3格式的实例代码
Oct 09 Python
如何使用Python脚本实现文件拷贝
Nov 20 Python
Python序列化pickle模块使用详解
Mar 05 Python
python计算列表元素与乘积详情
Aug 05 Python
Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项
Oct 11 #Python
Pycharm 2019 破解激活方法图文详解
Oct 11 #Python
python 扩展print打印文件路径和当前时间信息的实例代码
Oct 11 #Python
python脚本调用iftop 统计业务应用流量的思路详解
Oct 11 #Python
Python3+Requests+Excel完整接口自动化测试框架的实现
Oct 11 #Python
python二进制读写及特殊码同步实现详解
Oct 11 #Python
Python+Tensorflow+CNN实现车牌识别的示例代码
Oct 11 #Python
You might like
php生成酷炫的四个字符验证码
2016/04/22 PHP
JavaScript语言中的Literal Syntax特性分析
2007/03/08 Javascript
javascript 写类方式之五
2009/07/05 Javascript
你必须知道的Javascript知识点之&quot;单线程事件驱动&quot;的使用
2013/04/23 Javascript
JavaScript函数的4种调用方法详解
2014/04/22 Javascript
js使用ajax读博客rss示例
2014/05/06 Javascript
用js读、写、删除Cookie代码续篇
2014/12/03 Javascript
javascript实现的固定位置悬浮窗口实例
2015/04/30 Javascript
Jquery时间轴特效(三种不同类型)
2015/11/02 Javascript
jQuery中的ready函数与window.onload谁先执行
2016/06/21 Javascript
jQuery实现磁力图片跟随效果完整示例
2016/09/16 Javascript
jQuery下拉菜单的实现代码
2016/11/03 Javascript
基于代数方程库Algebra.js解二元一次方程功能示例
2017/06/09 Javascript
JavaScript函数apply()和call()用法与异同分析
2018/08/10 Javascript
vue.js引入外部CSS样式和外部JS文件的方法
2019/01/06 Javascript
详解js获取video任意时间的画面截图
2019/04/17 Javascript
vue中 this.$set的用法详解
2019/09/06 Javascript
python 切片和range()用法说明
2013/03/24 Python
Python基于pillow判断图片完整性的方法
2016/09/18 Python
Python爬虫文件下载图文教程
2018/12/23 Python
解决python super()调用多重继承函数的问题
2019/06/26 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
2019/08/06 Python
对python中 math模块下 atan 和 atan2的区别详解
2020/01/17 Python
判断Threading.start新线程是否执行完毕的实例
2020/05/02 Python
Django REST 异常处理详解
2020/07/15 Python
Python在字符串中处理html和xml的方法
2020/07/31 Python
pycharm激活方法到2099年(激活流程)
2020/09/22 Python
浅析Python 字符编码与文件处理
2020/09/24 Python
Django搭建项目实战与避坑细节详解
2020/12/06 Python
历史学专业个人的自我评价
2013/10/13 职场文书
艺术应用与设计个人的自我评价
2013/11/23 职场文书
中学生团员自我评价分享
2013/12/07 职场文书
销售主管竞聘书
2014/03/31 职场文书
2014年新教师工作总结
2014/11/08 职场文书
初中生300字旷课检讨书
2014/11/19 职场文书
2016关于读书活动的心得体会
2016/01/14 职场文书