Newbie question: Dictionary of lists

Kossmann, Bill BKossmann at dthr.ab.ca
Wed Feb 16 17:50:55 CET 2000


Hello, Python gurus!

As part of my first Python effort, I need to implement a dictionary of
lists.  Basically, I need to read a history file containing account codes,
period names, and amounts; the dictionary's key is a concatenation of the
account codes, and the values are 12-element lists (one for each month).  I
want a sample dictionary entry to look like this:

	{'00006101071115000000': [10, 20, 30 ... 120]}

so that when I need to access March data for cost centre
'00006101071115000000', all I have to do something like:

	history['00006101071115000000'][2]

Trouble is, when I implement my FOR loop as below, *all* of the dictionary's
January elements are updated to the value of the last January item in the
history file.  I've searched high and low for a solution, but I'm stumped.  

I've put the relevant code and the input file inline below my signature;
does anyone see what I'm doing wrong here?  Thanks very much for any hints
or pointers you can offer!  

Yours in gratitude,

Bill

p.s. I'm running Python 1.5.2 on NT 4

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bill Kossmann, CGA
Business Analyst
David Thompson Health Region
Red Deer, Alberta, Canada
ph  403 343 4463
fx   403 343 4697 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# TEST.PY ============================================================

import string

history = {}
history['DEFAULT'] = [31,29,31,30,31,30,31,31,30,31,30,31]

# Null elements - one for each month of the year (Jan ... Dec)
nullList = ['', '', '', '', '', '', '', '', '', '', '', '']

# Short test file (16000 lines in production)
histFile = open("2000.txt", "r")
for histLine in histFile.readlines():
   histRcd = string.split(histLine, ",")

# Key is our cost centre code
   dictionaryKey = histRcd[0]+histRcd[1]+histRcd[2]+histRcd[3]

# Null list required to add an element if it's not the first element
   if not history.has_key(dictionaryKey):
      history[dictionaryKey] = nullList

# Obviously must evaluate for other months, but January will do for testing
   if (histRcd[4][:3] == 'JAN'):
      history[dictionaryKey][0] = float(string.strip(histRcd[5]))

# Print out stuff for debugging as data is read into dictionary
      print history, '\n'

# Check the results
print '\n'
for data in history.keys():
   print data, '\t', history[data], '\n'

histFile.close()

# 2000.TXT ===========================================================

00,006,1010,71115000000,APR-00,116086.78
00,006,1010,71115000000,AUG-00,609247.99
00,006,1010,71115000000,DEC-00,1137682.76
00,006,1010,71115000000,FEB-00,1061321.95
00,006,1010,71115000000,JAN-00,1060712.34
00,006,1010,71115000000,JUL-00,503767.58
00,006,1010,71115000000,JUN-00,377198.08
00,006,1010,71115000000,MAY-00,264693.16
00,006,1010,71115000000,NOV-00,1004264.5
00,006,1010,71115000000,OCT-00,868372.67
00,006,1010,71115000000,SEP-00,739740.11
00,006,1010,71115900000,APR-00,17508.32
00,006,1010,71115900000,AUG-00,-328862.2
00,006,1010,71115900000,DEC-00,-199854.91
00,006,1010,71115900000,FEB-00,-138890.05
00,006,1010,71115900000,JAN-00,-138890.05
00,006,1010,71115900000,JUL-00,285846
00,006,1010,71115900000,JUN-00,257857.71
00,006,1010,71115900000,MAY-00,227246.56
00,006,1010,71115900000,NOV-00,-293133.9
00,006,1010,71115900000,OCT-00,-364361.44
00,006,1010,71115900000,SEP-00,-431565.48
00,006,1010,71115950000,APR-00,-2751.23
00,006,1010,71115950000,AUG-00,56302.45
00,006,1010,71115950000,DEC-00,87618.3
00,006,1010,71115950000,FEB-00,87618.3
00,006,1010,71115950000,JAN-00,87618.3
00,006,1010,71115950000,JUL-00,-3098.64
00,006,1010,71115950000,JUN-00,-13041.97
00,006,1010,71115950000,MAY-00,29.51
00,006,1010,71115950000,NOV-00,76108.28




More information about the Python-list mailing list