dictionary confusion...

mirvine555 mirvine at compsoc.com
Sat Jul 13 19:55:45 EDT 2002


Hi,

I'm trying to use dictionarys to store a table. The table looks like:
Data    mc1    mc2    mc3
hot    1:35   1:22      .
cold      .      .   7:43
dry   10:25  10:38   4:17

and I'm trying to put it into a dictionary something like:

>>> statusTable={'mc1':{'hot':'1:35',
			'cold':'.',
			'dry':'10:25'},
		'mc2':{'hot':'1:22',
			'cold':'.',
			'dry':'10:38'},
		'mc3':{'hot':'.',
			'cold':'7:43',
			'dry':'4:17'}}
>>> print statusTable
{'mc3': {'dry': '4:17', 'hot': '.', 'cold': '7:43'}, 'mc2': {'dry':
'10:38', 'hot': '1:22', 'cold': '.'}, 'mc1': {'dry': '10:25', 'hot':
'1:35', 'cold': '.'}}

I want to do this so I can look up values like this:

>>> statusTable['mc3']['cold']
'7:43'
>>> statusTable['mc2']['dry']
'10:38'
>>> 

I've made a feeble effort, as listed below, but I'm new to python so
I'm a bit stuck. If you have any ideas on how I can get this to work I
would appreciate your help.

TIA,
Mark Irvine

import string
def makeStatusTable(data):
    statusTable={}
    
    index=0
    lines=string.split(data,'\n')
    header=string.split(lines[0])[1:]    

    for mc in header:
        index+=1
        for line in lines[1:]:
        
            vals=string.split(line)
            status=vals[0]
            time=vals[index]
            print "vals: " + str(vals) + "\n"
                           

            statusTable[mc]=mc
            statusTable[mc]={status:time}

    return statusTable



data="""Data    mc1    mc2    mc3
hot    1:35   1:22      .
cold      .      .   7:43
dry   10:25  10:38   4:17"""

t=makeStatusTable(data)
print t



More information about the Python-list mailing list