开发者

How to pass items as args lists in map?

This is a piece of my code. Lambda accepts 3 parameters, and I wanted to pass them as a tuple of positional arguments, but apparently map supplies them as a single argument.

How can I supply those tuples in the bot开发者_运维技巧tom as lists of arguments? (I know I can rewrite the lambda, but it will become not well readable)

 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')))


The map() description:

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.

You need to place the arguments in parallel lists or tuples, and pass them to map() as 3 iterables.


Try putting parens around them

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')))


An alternative is to use itertools.starmap which accepts pre-zipped arguments:

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')))


One way is to rewrite as a list comprehension:

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'))]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜