python中reduce用法
2018-07-20 來源:open-open

>>> def myfunction(a,b): ... return a*b ... >>> mylist = [1,2,3,4,5] >>> print reduce(myfunction, mylist) 120 #上面的代碼相當于: >>>print ((((1*2)*3)*4)*5) 120 #也可以直接使用操作符模塊來替代函數(shù) >>> import operator >>> mylist = [1,2,3,4,5] >>> print reduce(operator.mul, mylist) 120 >>> print reduce(operator.add, mylist) 15
標簽: 代碼
版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。
最新資訊
熱門推薦