List mapping?

Mike Fletcher mfletch at tpresence.com
Thu Apr 13 10:33:25 EDT 2000


not elegant, but here's another way...

original = string.split( x, ' ')
result = []
while original:
	result.append( tuple(original[:2]) )
	del original[:2]

and another...

original = string.split( x, ' ')
result = []
for x in range(0, len(original), 2 ):
	result.append( tuple( original[x:x+2]))

HTH,
Mike

-----Original Message-----
From: Nick Trout [mailto:nick at spam_me_notvideosystem.co.uk]
Sent: Thursday, April 13, 2000 10:10 AM
To: python-list at python.org
Subject: List mapping?


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?!!  :-)

Thanks in advance,
Nick.





-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list