How to store "3D" data? (data structure question)

Cyril Bazin cyril.bazin at gmail.com
Wed Jul 20 21:52:15 CEST 2005


The question of the type of the data sutructure depends of your use of the 
data. 
You could avoid some confusion without naming your columns "lines"...

Anyway, here is a piece of code that read the file and count the star on the 
fly:
(The result is a dict of dict of int.)

-----------------------------------------------------------------
import itertools
import csv

f = open("toto.data") #change your file name
lineReader = csv.reader(f)

#Set the lines titles (RHA280, etc)
l = lineReader.next() 
linesTitles = l[2:]

#We construct the data structure and count the stars on the fly
datas = {}
for l in lineReader:
name, allele = l[:2]
if not allele: #empty line
continue
if name: #new block defining a TD*
currentName = name
d = dict.fromkeys(linesTitles, 0)
datas[currentName] = d
lines = l[2:]
#add 1 to the lines not empty (<=> with a star)
for title, value in itertools.izip(linesTitles,lines): 
if value:
d[title] += 1

#little tests
print datas
print datas["TDF1"]["RHA280"]
print datas["TDF3"]["RHA280"]
-----------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050720/76ef6d58/attachment.html>


More information about the Python-list mailing list