亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么這里的reduce 函數需要加上第三個參數?

def calc_prod(lst):
??? def lazy_prod():
?????? def f(x,y):
?????????? return x*y
??????? return reduce(f,1st,1)
??????? #return reduce(f,1st)

??? return lazy_prod

f = calc_prod([1, 2, 3, 4])
print f()

正在回答

1 回答

reduce(function,?sequence[,?initial])?->?value

Apply?a?function?of?two?arguments?cumulatively?to?the?items?of?a?sequence,
from?left?to?right,?so?as?to?reduce?the?sequence?to?a?single?value.
For?example,?reduce(lambda?x,?y:?x+y,?[1,?2,?3,?4,?5])?calculates
((((1+2)+3)+4)+5).??If?initial?is?present,?it?is?placed?before?the?items
of?the?sequence?in?the?calculation,?and?serves?as?a?default?when?the
sequence?is?empty.

自己解讀下這句話,應該對你有幫助 If initial is present, it is placed before the items?of the sequence in the calculation, and serves as a default when the?sequence is empty.?

0 回復 有任何疑惑可以回復我~
#1

章然

完整的部分在哪里可以找到呢?我想看看。
2015-04-25 回復 有任何疑惑可以回復我~
#2

DanDanHang 回復 章然

完整的部分值的是以上那段話完整的部分嗎?如果是的話,你可以在你python的命令行界面下,輸入 help(reduce)
2015-04-25 回復 有任何疑惑可以回復我~
#3

章然 回復 DanDanHang

非常感謝。
2015-04-25 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
python進階
  • 參與學習       255563    人
  • 解答問題       3038    個

學習函數式、模塊和面向對象編程,掌握Python高級程序設計

進入課程

為什么這里的reduce 函數需要加上第三個參數?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號