[Tutor] iterating data and populating a dictionary

Bryan Fodness bryan.fodness at gmail.com
Tue Aug 5 17:08:57 CEST 2008


i am filling a dictionary with a dictionary and my values for
isegment[field] are identical.  i can't see where i am overwriting the
previous field values.

my data looks like

Field = aa1
Index = 0.0
Value = 0.0
...
...
 Field = aa2
Index = 0.01
Value = 0.5
...


i would like to have something like,  {1: {'Value': 0.0, ...}, 2: {'Value':
0.01, ...}}

for line in file(data_file):
    the_line = line.split()
    if the_line:
        if the_line[0] == 'Field':
            field += 1
        elif the_line[0] == 'Index':
            index = float(the_line[-1])
            dif_index = index - start_index
            iindex[field] = dif_index
            start_index = index
        elif the_line[0] == 'Value':
            segment[the_line[1]] = float(the_line[-1])*(ax/40)
            isegment[field] = segment


-- 
"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080805/3f4b7c28/attachment.htm>


More information about the Tutor mailing list