为什么减少函数的参数

时间:2019-04-20 18:12:08

标签: python-3.x

我已经用reduce内置函数编写了这些代码行,但是对于给定的参数它显示了一个错误。

错误

TypeError跟踪(最近一次通话)  在       4       5 lst = [1,2,3] ----> 6 reduce(d_n,lst)

TypeError:d_n()接受1个位置参数,但给出了2个


from functools import reduce
def d_n(digit):
    return(digit)

lst = [1,2,3]
reduce(d_n, lst)

1 个答案:

答案 0 :(得分:0)

<table id="hosts" border="1">
  <tbody>
    <tr>
      <td><input type="checkbox" checked=""></td>
      <td>127.0.0.1</td>
      <td>localhost</td>
    </tr>
    <tr>
      <td><input type="checkbox"></td>
      <td>255.255.255.255</td>
      <td>broadcasthost</td>
    </tr>
  </tbody>
</table>

关键点:具有两个自变量的功能

您的reduce(...) 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. 函数仅接受一个参数,这使其与d_n()不兼容