views:

100

answers:

4

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 bottom 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')))
+2  A: 

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')))
gnibbler
GREAT! Can't believe it's so simple!
culebrón
Note that this approach is not valid in Python > 3.0 where parameter deconstruction of this type is no longer supported.A function f(a, (b, c): return a+b+cneeds to be written f(a, b_c): b, c = b_c return a+b+cRegards,Rickard
Rickard
+1  A: 

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'))]
Greg Hewgill
A: 

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.

gimel
Yes, this is correct, but the code (the tuples) will be unreadable.
culebrón
+2  A: 

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