Skip to main content
 首页 » 编程设计

python之如何在 map 中将项目作为参数列表传递

2025年05月04日25grandyang

这是我的一段代码。 Lambda 接受 3 个参数,我想将它们作为位置参数的元组传递,但显然 map 将它们作为单个参数提供。

如何在底部提供那些元组作为参数列表? (我知道我可以重写 lambda,但它的可读性会变得不好)

 adds = map((lambda j, f, a: 
      j.join([f.format(i) for i in parse.options[a]]) if parse.options[a] else ''), 
      ((' ', ' -not -path "{0}" ', 'exclude'), 
      (' -or ', '-path "{0}"', 'include'))) 

请您参考如下方法:

map()描述:

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

您需要将参数放在并行列表或元组中,并将它们传递给 map()作为 3 个可迭代对象。