Python dictionary syntax, need help

Karl Scalet news at
Mon Jul 21 10:15:23 CEST 2003

Crew Reynolds schrieb:
> I want to create a dictionary of note on/off timings and access it by
> the name of the object. The following code works for one row.
> a = "Hammer1" 
> b = [5,45,45,45,45,50,55,57,59,61,60,59] 
> notes = {a:b} 
> print notes["Hammer1"][3] 
> The problem is, I want to load the data from a file to populate
> variables a and b and build a dictionary for "Hammer1" thru
> "Hammer10". That way, I can stick the name of the object in the array
> index and receive a list of note timings that I can index with an
> ordinal as in the above example.
> This is straight Python syntax but I'm hoping someone has done this or
> understands the syntax better than I.
> Thanks! Alternative solutions to this concept would be greatly
> appreciated.

If there is no special format required for the text file, you could
invest a bit more in typing this text file, say, 'content.inp' like:

notes['hammer1'] = [1,2,3,4]
notes['hammer2'] = [2,3,4,5]
notes['hammer3'] = [3,4,5,6]

which on the other hand would simplify your code to:

notes = {}
# now notes is initialized with the content

I often do configurations/initialization stuff like this,
just using legal python syntax for such files.

Of course, my approach is not far from hardcoding the content
in the first place.


More information about the Python-list mailing list