詳解Python3遷移接口變化采坑記
1、除法相關(guān)
在python3之前,
print 13/4 #result=3
然而在這之后,卻變了!
print(13 / 4) #result=3.25
"/”符號運算后是正常的運算結(jié)果,那么,我們要想只取整數(shù)部分怎么辦呢?原來在python3之后,“//”有這個功能:
print(13 // 4) #result=3.25
是不是感到很奇怪呢?下面我們再來看一組結(jié)果:
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()函數(shù)中cmp參數(shù)發(fā)生了變化(重要)
在python3之前:
def reverse_numeric(x, y): return y - x print sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
輸出的結(jié)果是:[5, 4, 3, 2, 1]
但是在python3中,如果繼續(xù)使用上面代碼,則會報如下錯誤:
TypeError: 'cmp' is an invalid keyword argument for this function
咦?根據(jù)報錯,意思是在這個函數(shù)中cmp不是一個合法的參數(shù)?為什么呢?查閱文檔才發(fā)現(xiàn),在python3中,需要把cmp轉(zhuǎn)化為一個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)
這樣才能輸出結(jié)果!
具體可參考鏈接:Sorting HOW TO
3、map()函數(shù)返回值發(fā)生了變化
Python 2.x 返回列表,Python 3.x 返回迭代器。要想返回列表,需要進行類型轉(zhuǎn)換!
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 匿名函數(shù) print(map(lambda x: x ** 2, [1, 2, 3, 4])) # <map object at 0x000001E553CDC1D0>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用python和Django完成博客數(shù)據(jù)庫的遷移方法
- Python依賴包遷移到斷網(wǎng)環(huán)境操作
- 如何把外網(wǎng)python虛擬環(huán)境遷移到內(nèi)網(wǎng)
- 如何將你的應(yīng)用遷移到Python3的三個步驟
- python 動態(tài)遷移solr數(shù)據(jù)過程解析
- python django生成遷移文件的實例
- Python依賴包整體遷移方法詳解
- pycharm使用正則表達式批量添加print括號完美從python2遷移到python3
- python虛擬環(huán)境遷移方法
- python實現(xiàn)數(shù)據(jù)庫跨服務(wù)器遷移
- 用python寫個博客遷移工具
相關(guān)文章
Tensorflow安裝問題: Could not find a version that satisfies the
這篇文章主要介紹了Tensorflow安裝問題: Could not find a version that satisfies the requirement tensorflow,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python如何在pygame中設(shè)置字體并顯示中文詳解
再簡單的游戲界面中均涉及文字處理,下面這篇文章主要給大家介紹了關(guān)于python如何在pygame中設(shè)置字體并顯示中文的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01Python爬蟲Xpath定位數(shù)據(jù)的兩種方法
這篇文章主要介紹了Python爬蟲Xpath定位數(shù)據(jù)的方法,第一種方法直接右鍵,將文章路徑復(fù)制下來點擊Copy?full?Xpath,方法二使用@制定標簽屬性,搜索指定位置,每種方法給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07python opencv 讀取圖片 返回圖片某像素點的b,g,r值的實現(xiàn)方法
今天小編就為大家分享一篇python opencv 讀取圖片 返回圖片某像素點的b,g,r值的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python實現(xiàn)讀取Excel文件并復(fù)制指定的數(shù)據(jù)行
這篇文章主要介紹了如何基于Python語言,讀取Excel表格文件數(shù)據(jù),并基于其中某一列數(shù)據(jù)的值,將這一數(shù)據(jù)處于指定范圍的那一行加以復(fù)制,感興趣的可以了解一下2023-07-07