a clean way to define dictionary
Matt Gerrans
mgerrans at mindspring.com
Wed Jun 18 02:13:03 EDT 2003
I just had a very similar task today; I would build your dictionary like
this:
items = {}
for pair in [line.split() for line in lst.split('\n')]:
if len(pair) == 2:
key,value = pair
items[key] = int(value) # (or long, if necessary)
(I'm not into the real cryptic one-liners, I don't want to discourage
potential newcomers to Python who may stumble across my code). Also, you
may want some exception handling if your input can't be trusted to always be
in the format shown.
By the way, the problem I was dealing with was a work around for a bug in
the Microsoft Visual Studio .Net has a strange bug that causes custom
controls to keep on automatically getting bigger every time you open a
module.
I created a list file containing modules I'm working on and the controls
that Visual Studio keeps on hosing up, along with their desired sizes, like
so:
d:\path\module1.cs userControl1 100 200
d:\path\module1.cs userControl2 130 230
d:\path\module2.cs userControlA 200 300
userControlA 200 300
d:\path\module3.cs userControlA 400 500
(in the case where no file is specified, it is assumed to be the one from
the last line where one was.)
I read this into a dictionary then run through all the files in the
dictionary's keys and repair their controls' sizes right before a build.
More information about the Python-list
mailing list