代码之家  ›  专栏  ›  技术社区  ›  culebrón

如何在地图中将项目作为参数列表传递?

  •  1
  • culebrón  · 技术社区  · 16 年前

    这是我的一段代码。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')))
    
    4 回复  |  直到 16 年前
        1
  •  3
  •   John La Rooy    16 年前

    试着把帕伦斯放在他们周围

    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')))
    
        2
  •  5
  •   Community Mohan Dere    6 年前

    这个 map() 说明:

    map(function, iterable, ...)

    将函数应用于iterable的每个项并返回结果列表。 若传递了额外的iterable参数,则函数必须接受那个么多的参数,并并行应用于所有iterable中的项 . 如果一个iterable比另一个iterable短,则假定扩展为无项。如果函数为None,则假定恒等式函数;如果有多个参数,map()将返回一个由元组组成的列表,其中包含来自所有iterables的相应项(一种转置操作)。iterable参数可以是序列或任何iterable对象;结果总是一个列表。

    您需要将参数放置在并行列表或元组中,并将其传递给 就像三个不可数名词一样。

        3
  •  3
  •   Rickard    16 年前

    adds = itertools.starmap((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')))
    
        4
  •  1
  •   Greg Hewgill    16 年前

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