[Tutor] Data persistence problem
ALAN GAULD
alan.gauld at btinternet.com
Sat Jun 22 01:56:53 CEST 2013
Just a wee thought:
if isinstance(dict(), typein):
> newdict = {}
> dl = instring.split()
> if len(dl) % 2 != 0: raise Exception ("list entries must be
>even") # so they match
> for idx in range(0,len(dl),2):
> newdict[dl[idx]] = dl[idx+1]
>The for loop can be replaced with:
newdict = dict(zip(L[::2],L[1::2]))
Which avoids the explicit arithmetic and indexing and is therefore, arguably,
cleaner... I'd probably wrap it in a try clause too:
if isinstance(dict(),typein):
try: newdict = dict(zip(dl[::2],dl[1::2]))
except TypeError:
raise ValueError("input lists must be an even length")
And, since you always split the input, move that above all the isinstance() tests...
Similarly you don't need the loops for lists or sets, just use:
newlist = dl
and
newset = set(dl)
But that doesn't answer your question about incrementing the globals! :-)
To me it looks from your sample data like it is working!
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130622/e79a35bd/attachment-0001.html>
More information about the Tutor
mailing list