📜  Python – 将多个参数传递给映射函数

📅  最后修改于: 2022-05-13 01:54:39.636000             🧑  作者: Mango

Python – 将多个参数传递给映射函数

map()函数是Python中的内置函数,它将给定函数应用于可迭代的每个项目(如列表、元组等)并返回结果列表或映射对象。

笔记 :

  1.  您可以将任意数量的可迭代对象传递给 map()函数。
  2. 确保该函数对每个可迭代对象都有一个参数。

例子 :

Python3
# Python program to show working
# of map() function
  
# Return cube of n
def cube(n):
    return n**3
  
# Taking list as iterator
evennum = [2,4,6,8]
res = map(cube,evennum)
print(list(res))


Python3
# Python program to demonstrate
# passing of multiple iterable arguments to map()
# using 2 lists
  
# Function which return sum of 2 numbers
def sum(a,b):
    return a+b
  
# list 1
lst1=[2,4,6,8]
  
# list 2
lst2=[1,3,5,7,9]
  
  
result=list(map(sum,lst1,lst2))
print(result)


Python3
# Python program to demonstrate
# passing of multiple iterable arguments to map()
# using 3 lists
  
# Function which return product of 2 numbers
def Multiply(a,b,c):
    return a*b*c
  
# list 1
lst1=[2,4,6,8,10,12,14,16]
  
# list 2
lst2=[1,3,5,7,9,11,15]
  
#list 3
lst3=[2,3,5,7,11,13,17]
  
  
result=list(map(Multiply,lst1,lst2,lst3))
print(result)


Python3
# Python program to demonstrate
# passing of multiple iterable arguments to map()
# using list and tuple
  
# Function which perform division of 2 numbers
def division(a,b):
    return a/b
  
# list 
lst=[2,4,6,8,10,12,14,16]
  
# tuple
tup=(2,3,5,7,9,11)
  
result=list(map(division,lst,tup))
print(result)


输出 :

[8, 64, 216, 512]

将多个参数传递给 map()函数

我们可以将多个可迭代参数传递给 map()函数。为此,必须遵守某些规则——

  • 假设我们将n 个可迭代对象传递给 map(),那么给定函数应该有n个参数。
  • 这些可迭代参数必须并行应用于给定函数。
  • 在多个可迭代参数中,当最短的可迭代被耗尽时,映射迭代器将停止。
  • 但是在Python 2 的情况下,映射迭代器将在最长序列完成时停止。

代码 1:将两个列表和“sum”函数传递给 map()。

方法:

  • 定义一个函数sum,它返回两个数字的和。
  • 声明和初始化 lst1 和 lst2。
  • 将 sum函数、lst1 和 lst2 传递给 map()。
  • 两个列表中索引 0 处的元素将作为参数传递给 sum函数,并将返回它们的总和。
  • 这个循环一直持续到一个列表的元素用完为止。
  • 结果将存储在结果列表中。

Python3

# Python program to demonstrate
# passing of multiple iterable arguments to map()
# using 2 lists
  
# Function which return sum of 2 numbers
def sum(a,b):
    return a+b
  
# list 1
lst1=[2,4,6,8]
  
# list 2
lst2=[1,3,5,7,9]
  
  
result=list(map(sum,lst1,lst2))
print(result)

输出 :

[3, 7, 11, 15]

代码 2:将三个列表和“乘”函数传递给 map()。

方法:

  • 定义一个函数Multiply,它返回三个数字的乘积。
  • 声明和初始化 lst1、lst2 和 lst3。
  • 将乘法函数、lst1、lst2 和 lst3 传递给 map()。
  • 所有三个列表中索引 0 处的元素将作为参数传递给 Multiply函数,并返回它们的乘积。
  • 这个循环一直持续到一个列表的元素用完为止。
  • 结果将存储在结果列表中。

Python3

# Python program to demonstrate
# passing of multiple iterable arguments to map()
# using 3 lists
  
# Function which return product of 2 numbers
def Multiply(a,b,c):
    return a*b*c
  
# list 1
lst1=[2,4,6,8,10,12,14,16]
  
# list 2
lst2=[1,3,5,7,9,11,15]
  
#list 3
lst3=[2,3,5,7,11,13,17]
  
  
result=list(map(Multiply,lst1,lst2,lst3))
print(result)

输出 :

[4, 36, 150, 392, 990, 1716, 3570]

代码 3:将“除法”函数、一个列表和一个元组传递给 map()。

方法 :

  • 定义和初始化列表和元组。
  • 定义对两个数进行除法的除法函数。
  • 将 lst、tup 和除法函数传递给 map()。
  • 列表和元组中索引 0 处的元素将作为参数传递给除法函数,并返回它们的商。
  • 这个循环一直持续到列表或元组的元素用完为止。
  • 结果将存储在结果列表中。

Python3

# Python program to demonstrate
# passing of multiple iterable arguments to map()
# using list and tuple
  
# Function which perform division of 2 numbers
def division(a,b):
    return a/b
  
# list 
lst=[2,4,6,8,10,12,14,16]
  
# tuple
tup=(2,3,5,7,9,11)
  
result=list(map(division,lst,tup))
print(result)

输出 :