List mapping?
Nick Trout
nick at spam_me_notvideosystem.co.uk
Thu Apr 13 11:01:38 EDT 2000
I came up with something similar:
processfn = { 'del':deletefiles }
comm = None
flist = []
fileopts = sys.argv[2:]
while fileopts:
if fileopts[0]=='/c':
comm = fileopts[1]
fileopts = fileopts[2:]
elif fileopts[0]=='/d':
fdir = fileopts[1]
ext = fileopts[2]
fileopts = fileopts[3:]
flist.extend( readdir(fdir,ext) )
else:
raise "Unknown flag:",fileopts[0]
processfn[comm](flist)
There are obviously a lot of ways to do this in Python, the most elegant way
doesnt always seem obvious at first! Just wondered how others would approach
this problem.
Cheers,
Nick.
"Mike Fletcher" <mfletch at tpresence.com> wrote in message
news:CC59868A0A76D311B1F50090277B321C1F326A at VRTMAIL...
> 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