解決Python3下map函數(shù)的顯示問題
map函數(shù)是Python里面比較重要的函數(shù),設(shè)計靈感來自于函數(shù)式編程。Python官方文檔中是這樣解釋map函數(shù)的:
map(function, iterable, ...)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.
即map函數(shù)接收的第一個參數(shù)為一個函數(shù),可以為系統(tǒng)函數(shù)例如float、或者def定義的函數(shù)、或者lambda定義的函數(shù)均可。
舉一個簡單的例子,下面這個例子在Python2.7下是可以正常顯示的:
ls = [1,2,3] rs = map(str, ls) #打印結(jié)果 ['1', '2', '3'] lt = [1, 2, 3, 4, 5, 6] def add(num): return num + 1 rs = map(add, lt) print rs #[2,3,4,5,6,7]
但是在Python3下我們輸入:
ls=[1,2,3] rs=map(str,ls) print(rs)
顯示的卻是:
<map at 0x3fed1d0>
而不是我們想要的結(jié)果,這也是Python3下發(fā)生的一些新的變化,如果我們想得到需要的結(jié)果需要這樣寫:
ls=[1,2,3] rs=map(str,ls) print(list(rs))
這樣顯示的結(jié)果即為我們想要看到的。這一點在《機器學(xué)習(xí)實戰(zhàn)》的第10章中會有一點幫助。
以上這篇解決Python3下map函數(shù)的顯示問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python數(shù)學(xué)建模是加深Numpy和Pandas學(xué)習(xí)
這篇文章主要介紹了python數(shù)學(xué)建模是加深Numpy和Pandas學(xué)習(xí),緊接上一篇學(xué)習(xí)內(nèi)容展開Numpy更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-07-07Python簡單獲取二維數(shù)組行列數(shù)的方法示例
這篇文章主要介紹了Python簡單獲取二維數(shù)組行列數(shù)的方法,結(jié)合實例形式分析了Python基于numpy模塊的二維數(shù)組相關(guān)運算技巧,需要的朋友可以參考下2018-12-12