用于使用参数运行的Python映射列表项

时间:2022-08-22 13:46:41

Is there any way to map list items to a function along with arguments. I have a list:

有没有办法将列表项与参数一起映射到函数。我有一个清单:

pages = [p1, p2, p3, p4, p5...]

And I have to call function myFunc corresponding to each list elements along with additional arguments such that the following can be computed

我必须调用与每个列表元素对应的函数myFunc以及其他参数,以便可以计算以下内容

myFunc(p1, additionalArgument)
myFunc(p2, additionalArgument)

and so on...

等等...

Is there any elegant method for doing this? I am a newbie so please don't mind.

这样做有什么优雅的方法吗?我是新手所以请不要介意。

3 个解决方案

#1


35  

Use a list comprehension:

使用列表理解:

result = [myFunc(p, additionalArgument) for p in pages]

#2


36  

You can also use a lambda function:

您还可以使用lambda函数:

map(lambda p: myFunc(p, additionalArgument), pages)

#3


31  

You could use a list comprehension

你可以使用列表理解

[myFunc(p, additionalArgument) for p in pages]

or functools.partial()

map(functools.partial(myFunc, some_arg=additionalArgument), pages)

#1


35  

Use a list comprehension:

使用列表理解:

result = [myFunc(p, additionalArgument) for p in pages]

#2


36  

You can also use a lambda function:

您还可以使用lambda函数:

map(lambda p: myFunc(p, additionalArgument), pages)

#3


31  

You could use a list comprehension

你可以使用列表理解

[myFunc(p, additionalArgument) for p in pages]

or functools.partial()

map(functools.partial(myFunc, some_arg=additionalArgument), pages)