What is the best way to implement nested dictionaries?

后端 未结 21 1843
[愿得一人]
[愿得一人] 2020-11-22 00:29

I have a data structure which essentially amounts to a nested dictionary. Let\'s say it looks like this:

{\'new jersey\': {\'mercer county\': {\'plumbers\':          


        
21条回答
  •  后悔当初
    2020-11-22 00:57

    Since you have a star-schema design, you might want to structure it more like a relational table and less like a dictionary.

    import collections
    
    class Jobs( object ):
        def __init__( self, state, county, title, count ):
            self.state= state
            self.count= county
            self.title= title
            self.count= count
    
    facts = [
        Jobs( 'new jersey', 'mercer county', 'plumbers', 3 ),
        ...
    
    def groupBy( facts, name ):
        total= collections.defaultdict( int )
        for f in facts:
            key= getattr( f, name )
            total[key] += f.count
    

    That kind of thing can go a long way to creating a data warehouse-like design without the SQL overheads.

提交回复
热议问题