详解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 相关文章推荐
10种检测Python程序运行时间、CPU和内存占用的方法
Apr 01 Python
python实现读取并显示图片的两种方法
Jan 13 Python
Python处理中文标点符号大集合
May 14 Python
pandas每次多Sheet写入文件的方法
Dec 10 Python
利用python实现在微信群刷屏的方法
Feb 21 Python
python实现五子棋小程序
Jun 18 Python
Python爬虫库BeautifulSoup的介绍与简单使用实例
Jan 25 Python
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
Feb 25 Python
CentOS 7如何实现定时执行python脚本
Jun 24 Python
python字典通过值反查键的实现(简洁写法)
Sep 30 Python
Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)
Dec 07 Python
Python实现socket库网络通信套接字
Jun 04 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环境配置 php5 MySQL5 apache2 phpmyadmin安装与配置图文教程
2007/03/16 PHP
让JavaScript 轻松支持函数重载 (Part 1 - 设计)
2009/08/04 Javascript
jquery聚焦文本框与扩展文本框聚焦方法
2012/10/12 Javascript
EditPlus注册码生成器(js代码实现)
2013/03/25 Javascript
Jquery EasyUI中弹出确认对话框以及加载效果示例代码
2014/02/13 Javascript
js操作IE浏览器弹出浏览文件夹可以返回目录路径
2014/07/14 Javascript
js实现C#的StringBuilder效果完整实例
2015/12/22 Javascript
JS中的作用域链
2017/03/01 Javascript
基于react框架使用的一些细节要点的思考
2017/05/31 Javascript
详解ElementUI之表单验证、数据绑定、路由跳转
2017/06/21 Javascript
vue使用stompjs实现mqtt消息推送通知
2017/06/22 Javascript
JavaScript对JSON数据进行排序和搜索
2017/07/24 Javascript
基于 Vue 的树形选择组件的示例代码
2017/08/18 Javascript
写给vue新手们的vue渲染页面教程
2017/09/01 Javascript
cropper js基于vue的图片裁剪上传功能的实现代码
2018/03/01 Javascript
微信小程序自定义多选事件的实现代码
2018/05/17 Javascript
微信小程序动态生成二维码的实现代码
2018/07/25 Javascript
vue-cli3 项目从搭建优化到docker部署的方法
2019/01/28 Javascript
Angular中使用ng-zorro图标库部分图标不能正常显示问题
2019/04/22 Javascript
微信小程序template模板与component组件的区别和使用详解
2019/05/22 Javascript
使用vue重构资讯页面的实例代码解析
2019/11/26 Javascript
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
2020/04/24 Javascript
Python读csv文件去掉一列后再写入新的文件实例
2017/12/28 Python
德国EGOIST网店:销售畅销的设计师品牌
2017/04/18 全球购物
Ruby如何创建一个线程
2013/03/10 面试题
优秀党员转正的自我评价
2013/10/06 职场文书
电子商务专业个人的自我评价分享
2013/10/29 职场文书
大学生就业推荐信范文
2013/11/29 职场文书
初中英语演讲稿
2014/04/29 职场文书
推广普通话共筑中国梦演讲稿
2014/09/21 职场文书
民间借贷协议书范本
2014/10/01 职场文书
单位作风建设自查报告
2014/10/23 职场文书
上市公司董事长岗位职责
2015/04/16 职场文书
八年级作文之我的母亲
2019/12/10 职场文书
Python装饰器详细介绍
2022/03/25 Python
解决ubuntu安装软件时,status-code=409报错的问题
2022/12/24 Servers