python - 使用多个列表作为函数的输入参数 (Python)

标签 python function input multiple-arguments

我知道属性 map(function,list) 将函数应用于单个列表的每个元素。但是,如果我的函数需要多个列表作为输入参数,那会怎样呢?。

例如我试过:

   def testing(a,b,c):
      result1=a+b
      result2=a+c
      return (result1,result2)

a=[1,2,3,4]
b=[1,1,1,1]
c=[2,2,2,2]

result1,result2=testing(a,b,c)

但这只是连接数组:

result1=[1,2,3,4,1,1,1,1]
result2=[1, 2, 3, 4, 2, 2, 2, 2]

我需要的是以下结果:

result1=[2,3,4,5] 
result2=[3,4,5,6]

如果有人能让我知道这怎么可能,或者给我指向一个链接,我的问题可以在类似案例中得到解答,我将不胜感激。

最佳答案

你可以使用operator.add

from operator import add

def testing(a,b,c):
    result1 = map(add, a, b)
    result2 = map(add, b, c)
    return (result1, result2)

关于python - 使用多个列表作为函数的输入参数 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045744/

相关文章:

Python:计算当前时间和上次登录之间的时间。 (自动通讯)

python - 在另一个 python 文件中访问 Flask 应用程序端点?

python - 将值作为索引函数分配给 2D numpy 数组的有效方法是什么

c++ - 读取空行 C++

python - 索引错误: list index out of range but I have more than enough elements?! Python

python - 在Python中检查( "and", "or")优先级的简单方法

javascript - For 循环不使用 JavaScript 函数的参数

php - 我想编写一个使用 $_POST ['first_name ' ] 和 $_POST ['password ' ] 作为参数的函数

c++ - 如何在 Qt LineEdit 上使用 .hide,仍然接受输入?

c - 每次我从 C 中的输入文件读取 '\n' 时都无法循环函数