problem of converting a list to dict

Mike Meyer mwm at mired.org
Wed Jan 9 16:01:18 EST 2008


On Wed, 9 Jan 2008 14:34:26 -0600 "Reedick, Andrew" <jr9445 at ATT.COM> wrote:

> > -----Original Message-----
> > From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> > list-bounces+jr9445=att.com at python.org] On Behalf Of John Machin
> > Sent: Wednesday, January 09, 2008 3:02 PM
> > To: python-list at python.org
> > Subject: Re: problem of converting a list to dict
> > 
> > On Jan 10, 6:52 am, "Reedick, Andrew" <jr9... at ATT.COM> wrote:
> > >
> > > A bigger hint:
> > >         a=i.split('=')
> > >         print "'%s' splits into " % (i), a
> > 
> > consider:
> > (1) using %r instead of '%s'
> 
> 	Eh, personal preference depending on how sure you are of the
> data's type. 
> 
> > (2) omitting the redundant space after 'into'
> 
> 	Some of us coming in from other languages and still aren't used
> to the comma adding an unwanted space after everything.  I've been
> tempted to root around in Python's source code to fix the problem.
> 
> > (3) losing the redundant () around i
> 
> 	For me, the () is there for readability.  Python's sprintf
> syntax is odd to begin with, and something like 
> 		print "'%s' splits into " % i, a, b, c
> 	means either
> 		1) you really do want to append b and c after the
> sprintf, or
> 			print "'%s' splits into " % (a), b, c
> 		2) that the formatting string is missing a few things 
> 			print "'%s' splits into " % (a, b, c)  ## Ooops!
> forgot to change it to "%s %5.2d %6.3f"

In that case, I'd suggest making the right operand a tuple:

   print "'%s' splits into" % (i,), a, b, c

which some will argue is good style in any case. Or if you just want
to aide readers not used to python, use the redundant parens to
enforce the default parsing:

   print ("'%s' splits into" % i), a, b, c

       <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.



More information about the Python-list mailing list