Data structure for maintaining tabular data in memory?

前端 未结 6 2006
孤城傲影
孤城傲影 2020-12-04 05:41

My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent,

6条回答
  •  爱一瞬间的悲伤
    2020-12-04 06:45

    Have a Table class whose rows is a list of dict or better row objects

    In table do not directly add rows but have a method which update few lookup maps e.g. for name if you are not adding rows in order or id are not consecutive you can have idMap too e.g.

    class Table(object):
        def __init__(self):
            self.rows =  []# list of row objects, we assume if order of id
            self.nameMap = {} # for faster direct lookup for row by name
    
        def addRow(self, row):
            self.rows.append(row)
            self.nameMap[row['name']] = row
    
        def getRow(self, name):
            return self.nameMap[name]
    
    
    table = Table()
    table.addRow({'ID':1,'name':'a'})
    

提交回复
热议问题