python 學習--map 和 reduce的使用
# 利用map()函式,把使用者輸入的不規範的英文名字,變為首字母大寫,其他小寫的規範名字。輸入:['adam', 'LISA', 'barT'],輸出:['Adam', 'Lisa', 'Bart']:
def normalize(name):
return name[0:1].upper()+name[1:].lower();
L1 = ['adam','LISA','barT']
L2 = list(map(normalize,L1))
print(L2)
# Python提供的sum()函式可以接受一個list並求和,請編寫一個prod()函式,可以接受一個list並利用reduce()求積:
from functools import reduce
def prod(L):
def cal(x,y):
return x*y
return reduce(cal,L)
print('3*5*7*9=',prod([3,5,7,9]))
# 利用map和reduce編寫一個str2float函式,把字串'123.456'轉換成浮點數123.456:
import math
def str2float(s):
def tonumber(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'-1':-1}[s.replace('.','-1')]
def positive(x,y):
if(y == -1):
return x
else:
return 10*x+y
return reduce(positive,map(tonumber,s))/math.pow(10,(len(s)-s.index('.')-1))
print('str2float(\'12123.321\')=',str2float('12123.321'))

相關文章
- Python學習筆記 - filter,map,reduce,zipPython筆記Filter
- 人人都能學會的python程式設計教程16:map和reducePython程式設計
- python-python的sao操作 map reduce filterPythonFilter
- JavaScript map和reduce的區別JavaScript
- reduce()方法的學習和整理
- python內建函式 map/reducePython函式
- Python中的Map、Reduce和Filter函數語言程式設計PythonFilter函數程式設計
- 陣列的 map, filter ,sort和 reduce 用法陣列Filter
- python之高階函式map,reduce,filter用法Python函式Filter
- JavaScript 4/30: 陣列的 map, filter 和 reduce 用法JavaScript陣列Filter
- Hadoop Map Reduce 漫談Hadoop
- forEach、map、reduce比較
- python中快速處理關鍵字map,reduce,filterPythonFilter
- [譯] 圖解 Map、Reduce 和 Filter 陣列方法圖解Filter陣列
- [翻譯]map和reduce,處理資料結構的利器資料結構
- 學習一下Array.reduce函式的使用函式
- python常用函式進階(2)之map,filter,reduce,zipPython函式Filter
- 在幕後看看Swift中的Map,Filter和Reduce的實現SwiftFilter
- Python中常用的幾個內建方法(max()/min()、filter()、map()、sorted、reduce())PythonFilter
- Python 進階之路 (五) map, filter, reduce, zip 一網打盡PythonFilter
- es6 的學習之 set 和 map
- 分散式計算與Map Reduce分散式
- kotlin之plus、copyOf、reverse、forEach、filter、map、reduce、fold等函式解釋和使用KotlinFilter函式
- 例項講解hadoop中的map/reduce查詢(python語言實現HadoopPython
- JS Array.reduce 實現 Array.map 和 Array.filterJSFilter
- 陣列的reduce操作+物件陣列的map操作陣列物件
- python map和lambdaPython
- Map-Reduce資料分析之二
- map、reduce、filter、for...of、for...in等總結Filter
- MyBatis學習筆記(四)使用map實現查詢和插入MyBatis筆記
- C++學習隨筆——使用map和迭代器iterator的簡單範例C++
- 陣列的forEach,map,filter,reduce,reduceRight,every,some方法陣列Filter
- STL的map使用和分析
- JavaScript reduce()的使用JavaScript
- javascript高階函式---filter---map---reduceJavaScript函式Filter
- 五、GO程式設計模式:MAP-REDUCEGo程式設計設計模式
- JavaScript(1)高階函式filter、map、reduceJavaScript函式Filter
- GO程式設計模式05:MAP-REDUCEGo程式設計設計模式
- 【大資料】深入原始碼解析Map Reduce的架構大資料原始碼架構