Arguments for map'ped functions

M

mk

Hello everyone,

So I have this function I want to map onto a list of sequences of
*several* arguments (while I would want to pass those arguments to each
function in the normal fashion). I realize this is contrived, maybe an
example would make this clear:

params = [ ('comp.lang.python', ['rtfm', 'shut']), ('comp.lang.perl',
['rtfm','shut']) ]

qurls = map(consqurls, params)

def consqurls(args):
ggroup, gkeywords = args
....

Since the argument passed to map'ped function is a single tuple of
arguments, I have to unpack them in the function. So the question is how
to pass several arguments to a map'ped function here?


Code in question:

def fillurlfmt(args):
urlfmt, ggroup, gkw = args
return {'group':ggroup, 'keyword':gkw, 'url': urlfmt % (gkw, ggroup)}

def consqurls(args):
ggroup, gkeywords = args
urlfmt =
'http://groups.google.com/groups/sea..._ugroup=%s&as_usubject=&as_uauthors=&safe=off'
qurls = map(fillurlfmt, [ (urlfmt, ggroup, gkw) for gkw in gkeywords])
return qurls

if __name__ == "__main__":
gkeywords = ['rtfm', 'shut']
ggroups = ['comp.lang.python', 'comp.lang.perl']
params = [(ggroup, gkeywords) for ggroup in ggroups]
qurls = map(consqurls, params)
print qurls

Regards,
mk
 
P

Peter Otten

mk said:
So I have this function I want to map onto a list of sequences of
*several* arguments (while I would want to pass those arguments to each
function in the normal fashion). I realize this is contrived, maybe an

You can either use a list comprehension

[f(*args) for args in seq]

or starmap()

list(itertools.starmap(f, seq))

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top