Python In-memory table

Posted by nsharish on Stack Overflow See other posts from Stack Overflow or by nsharish
Published on 2010-04-02T06:52:33Z Indexed on 2010/04/02 7:03 UTC
Read the original article Hit count: 395

Filed under:
|
|
|

What is the right way to forming in-memory table in python with direct lookups for rows and columns.
I thought of using dict of dicts this way,

class Table(dict):
    def __getitem__(self, key):
        if key not in self:
             self[key]={}
        return dict.__getitem__(self, key)
table = Table()
table['row1']['column1'] = 'value11'
table['row1']['column2'] = 'value12'
table['row2']['column1'] = 'value21'
table['row2']['column2'] = 'value22'
>>>table
{'row1':{'column1':'value11','column2':'value12'},'row2':{'column1':'value21','column2':'value22'}}

I had difficulty in looking up for values in columns.

>>>'row1' in table
True
>>>'value11' in table['row1'].values()
True

Now how do I do lookup if 'column1' has 'value11'
Is this method of forming tables wrong?
Is there a better way to implement such tables with easier lookups?.
Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about tables