使用带有关键字参数的map()函数

时间:2023-01-29 23:21:05

Here is the loop I am trying to use the map function on:

这是我尝试使用map函数的循环:

volume_ids = [1,2,3,4,5]
ip = '172.12.13.122'
for volume_id in volume_ids:
    my_function(volume_id, ip=ip)

Is there a way I can do this? It would be trivial if it weren't for the ip parameter, but I'm not sure how to deal with that.

有没有办法可以做到这一点?如果它不是ip参数,那将是微不足道的,但我不知道如何处理它。

5 个解决方案

#1


62  

Use functools.partial():

from functools import partial

mapfunc = partial(my_function, ip=ip)
map(mapfunc, volume_ids)

partial() creates a new callable, that'll apply any arguments (including keyword arguments) to the wrapped function in addition to whatever is being passed to that new callable.

partial()创建一个新的可调用对象,除了传递给新的可调用对象之外,它还将任何参数(包括关键字参数)应用于包装函数。

#2


11  

Here is a lambda approach (not better, just different)

这是一种lambda方法(不是更好,只是不同)

volume_ids = [1,2,3,4,5]
ip = '172.12.13.122'
map(lambda ids: my_function(ids, ip), volume_ids);

#3


5  

This can be done easily with a list comprehension.

这可以通过列表理解轻松完成。

volume_ids = [1,2,3,4,5]
ip = '172.12.13.122'
results = [my_function(i,ip=ip) for i in volume_ids]

#4


0  

How about this?

这个怎么样?

results = []
for volume_id in volume_ids:
    results.append(my_function(volume_id, ip=ip))

This is three lines of code instead of one --- it's three lines of clear and obvious code instead of importing some special-case helper from module such-and-such. This argument is probably a matter of taste, but it has a lot of weight depending on who you talk to.

这是三行代码而不是一行 - 它是三行清晰而明显的代码,而不是从模块中导入一些特殊情况的帮助程序。这个论点可能是一个品味问题,但它有很多权重取决于你与谁交谈。

#5


0  

In general, one can use map to pass keywords to a function by wrapping that function in something which unpacks a dictionary, and then passing an iterable of dictionaries to map. Example:

通常,可以使用map将关键字传递给函数,方法是将该函数包装在解包字典的内容中,然后将可迭代的字典传递给map。例:

from itertools import product

volume_ids = [1,2,3,4,5]
volume_ids = (("volume_id", volume_id) for volume_id in volume_ids)
ips = [("ip", '172.12.13.122')]
kwargs_iterable = map(dict, product(volume_ids, ips))

result = map(lambda kwargs: my_function(**kwargs), kwargs_iterable)

For your special case, however, a simpler solution would be:

但是,对于您的特殊情况,更简单的解决方案是:

map(my_function, volume_ids, [ip]*len(volume_ids))

This is concise and does not rely on any imports. Another possibility could be to combine product and starmap from itertools:

这很简洁,不依赖任何进口。另一种可能性是将来自itertools的产品和星图组合在一起:

from itertools import product, starmap

ips = [ip]
starmap(my_function, product(volume_ids, ips))

This generalizes nicely to the setting with more than one ip adress, or more than two variables.

这很好地概括了具有多个ip地址或两个以上变量的设置。

#1


62  

Use functools.partial():

from functools import partial

mapfunc = partial(my_function, ip=ip)
map(mapfunc, volume_ids)

partial() creates a new callable, that'll apply any arguments (including keyword arguments) to the wrapped function in addition to whatever is being passed to that new callable.

partial()创建一个新的可调用对象,除了传递给新的可调用对象之外,它还将任何参数(包括关键字参数)应用于包装函数。

#2


11  

Here is a lambda approach (not better, just different)

这是一种lambda方法(不是更好,只是不同)

volume_ids = [1,2,3,4,5]
ip = '172.12.13.122'
map(lambda ids: my_function(ids, ip), volume_ids);

#3


5  

This can be done easily with a list comprehension.

这可以通过列表理解轻松完成。

volume_ids = [1,2,3,4,5]
ip = '172.12.13.122'
results = [my_function(i,ip=ip) for i in volume_ids]

#4


0  

How about this?

这个怎么样?

results = []
for volume_id in volume_ids:
    results.append(my_function(volume_id, ip=ip))

This is three lines of code instead of one --- it's three lines of clear and obvious code instead of importing some special-case helper from module such-and-such. This argument is probably a matter of taste, but it has a lot of weight depending on who you talk to.

这是三行代码而不是一行 - 它是三行清晰而明显的代码,而不是从模块中导入一些特殊情况的帮助程序。这个论点可能是一个品味问题,但它有很多权重取决于你与谁交谈。

#5


0  

In general, one can use map to pass keywords to a function by wrapping that function in something which unpacks a dictionary, and then passing an iterable of dictionaries to map. Example:

通常,可以使用map将关键字传递给函数,方法是将该函数包装在解包字典的内容中,然后将可迭代的字典传递给map。例:

from itertools import product

volume_ids = [1,2,3,4,5]
volume_ids = (("volume_id", volume_id) for volume_id in volume_ids)
ips = [("ip", '172.12.13.122')]
kwargs_iterable = map(dict, product(volume_ids, ips))

result = map(lambda kwargs: my_function(**kwargs), kwargs_iterable)

For your special case, however, a simpler solution would be:

但是,对于您的特殊情况,更简单的解决方案是:

map(my_function, volume_ids, [ip]*len(volume_ids))

This is concise and does not rely on any imports. Another possibility could be to combine product and starmap from itertools:

这很简洁,不依赖任何进口。另一种可能性是将来自itertools的产品和星图组合在一起:

from itertools import product, starmap

ips = [ip]
starmap(my_function, product(volume_ids, ips))

This generalizes nicely to the setting with more than one ip adress, or more than two variables.

这很好地概括了具有多个ip地址或两个以上变量的设置。