【问题标题】:python loop function to loop another function over a listpython循环函数在列表上循环另一个函数
【发布时间】:2016-05-30 05:22:31
【问题描述】:

我有一个名为add_ten 的函数。我想在一个列表中循环这个函数。 我想创建一个looper 函数并将listadd_ten 函数作为输入循环传递

def add_ten(x):
    y = x+10
    print 'value of x is ' + str(x)
    print 'value of y is ' + str(y)

def looper(list,func):
    for i in list:
        return func(i)

我调用looper函数

looper([1,2],add_ten())

我收到此错误。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-6510e446c709> in <module>()
----> 1 looper([1,2],add_ten())

TypeError: add_ten() takes exactly 1 argument (0 given)

如何正确创建 looper 函数,以便它接受正确的参数?

【问题讨论】:

    标签: python list function loops


    【解决方案1】:

    为了理解这个问题,我得到了以下工作代码:

    #! /usr/bin/env python
    from __future__ import print_function
    
    
    def add_ten(x):
        print('value of x is %s\nvalue of y is %s' % (str(x), str(x + 10)))
    
    
    def looper(list, func):
        for i in list:
            func(i)
    
    if __name__ == '__main__':
        looper([1, 2], add_ten)
    

    输出在我的机器上:

    value of x is 1
    value of y is 11
    value of x is 2
    value of y is 12
    

    希望这有助于学习 Python ;-)

    注意looperyou 的实现肯定不希望在第一次迭代中尽早返回,因为这样只会处理第一个列表元素并得到结果。在上面我只是调用它以及打印的副作用或add_ten“happens”中定义的任何内容......

    【讨论】:

      【解决方案2】:

      TypeError: add_ten() takes exactly 1 argument (0 given)

      您收到此错误是因为您在没有参数的情况下调用函数 add_ten()


      我想创建一个looper 函数并将listadd_ten 函数作为输入循环传递。

      所以只需在 looper 函数中传递函数本身而不执行它,如下所示:

      looper([1,2], add_ten)
      

      然后,使用 i 值在 for 循环中调用函数:

      def looper(list, func):
          for i in list:
              return func(i)   # <--- notice the parentheses instead of square brackets.
      

      不过,我不确定return func(i) 的用途,因为它没有返回任何内容。所以,如果你只需要在add_ten函数中打印,你可以从这里删除return

      def looper(list, func):
          for i in list:
              func(i)
      

      【讨论】:

        【解决方案3】:

        仔细看看你得到的错误:“add_ten() 只需要 1 个参数(给定 0)”。

        这是因为在 return func[i] 你应该这样做:return func(i)(注意不同的括号) .

        否则,调用funct() 的参数就没有了。

        【讨论】:

          【解决方案4】:
          1. 当它是参数时不要调用函数

          looper([1,2], add_ten)

          1. 调用func(i) 时使用圆括号代替方括号。

          否则,您的代码看起来不错。

          【讨论】:

            【解决方案5】:

            使用 looper([1,2],add_ten()) 您传递的是函数调用的结果,而不是函数本身。

            固定版本如下:

            def add_ten(x):
                y = x+10
                print 'value of x is ' + str(x)
                print 'value of y is ' + str(y)
            
            def looper(list,func):
                for i in list:
                    return func(i)
            
            looper([1,2],add_ten)
            

            还要注意,您的 add_ten 函数始终返回 None 并且除了打印原始值和增量值之外没有任何效果。实际返回修改后列表的紧凑方式可能如下所示:

            def add_ten(x):
                return x + 10
            
            def looper(list,func):
                return [ func(i) for i in list ]
            
            result = looper([1,2],add_ten)
            print result
            

            【讨论】:

              猜你喜欢
              • 2015-04-22
              • 2019-10-18
              • 2023-02-06
              • 2019-12-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-20
              • 2011-09-18
              相关资源
              最近更新 更多