List mapping?

Robert Cragie rcc at nospamthanks_jennic.com
Thu Apr 13 17:01:49 CEST 2000


Nick Trout <nick at spam_me_notvideosystem.co.uk> wrote in message
news:38f5d68e at news.xtml.co.uk...
> Is there a nice way of pairing up the members to form tuple pairs in
another
> list? ie. like you might to processing input args.
>
> ie. changing ['/a','1','/b','2','/c','3'] into [ ('/a','1'), ('/b','2'),
> ('/c','3') ]
>
> eg:
>
> args = '/a 1 /b 2 /c 3'
> listargs= string.split(args)
> tuplist = []
> i=0
> while i < len(listargs):
>     tuplist.append( (listargs[i],listargs[i+1]) )
>     i = i+2
> for i in tuplist:
>     processargs(i)
>
> or:
>
> args = '/a 1 /b 2 /c 3'
> for i in string.split(args):
>     i = argflag
>     # i want next arg now as a parameter!!!
>
> Can I (you!) get rid of the long winded while loop cleverly?!!  :-)

Try this:
mylist = ['/a','1','/b','2','/c','3']
map(lambda x: reduce(lambda a, b: (a,b), mylist[x:x+2]), range(0,
len(mylist), 2))

Robert Cragie





More information about the Python-list mailing list