Dealing with MULTIDIMENSIONAL dictionaries python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saidi Mwanundu
    New Member
    • Mar 2012
    • 1

    Dealing with MULTIDIMENSIONAL dictionaries python

    Completely new to python and wondering how i can do this problem with multidimensiona l dictionaries. problem is to go in some file which has a counties information on a line with its state as well.
    the objective is to prompt for a state and the program tells you which of the counties has the most (not necessary for me to explain) of one of the details. what i want to do is create a dictionary that takes as key the state and as value a dictionary(call ed state_dict). Then i want state dict to take county name as key and to take another dictionary as value(county_di ct). and then county_dict to take 4 keys of info and their values as their appropriate values.
    what i have so far is
    Code:
    state=[]
    usa_dict = {}
    state_dict = {}
    county_dict = {}
    data = open("est10ALL.txt","rU")
    for line in data:
      line=line.strip()
      line=line.split()
      state.append(line)
         if line[-3] not in usa_dict:
             usa_dict[line[-3]]= state_dict
             if line[-5] not in state_dict:
                   state_dict[line[-5]] = county_dict
    Last edited by bvdet; Mar 19 '12, 11:50 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It would be helpful if you would post 2 or 3 sample lines from your file.

    Is this what you what your dictionary to look like?
    Code:
    {"Tennessee": {"Davidson": {'key1': value1, 'key2': value2, 'key3': value3, 'key4': value4}}}

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Generally, SQLite (comes with Python) works better than multi-dimensional dictionaries-->tutorial.
      Code:
      import sqlite3 as sqlite
      
      class SQLiteTest():
          def __init__(self):
              # Create a connection to the (memory) database file
              self.con = sqlite.connect(':memory:')
      
              # Get a Cursor object that operates in the context of Connection con
              self.cur = self.con.cursor()
         
              self.cur.execute("CREATE TABLE test_it (state varchar, county varchar, data varchar)")
      
          ##----------------------------------------------------------------------
          def add_rec(self, state_in, county_in, add_var):
              self.cur.execute("insert into test_it values (?, ?, ?)", (state_in, county_in, add_var))
              self.con.commit()
      
          def lookup_county(self, county_in):
              ## lookup using a tuple
              result=self.cur.execute('SELECT * FROM test_it where county=?', (county_in,))
              recs_list=result.fetchall()
              print recs_list
      
          def lookup_data_and_county(self, county_in, data_in):
              ## lookup using a dictionary
              result=self.cur.execute('SELECT * FROM test_it where county=:dict_county and data=:dict_data', \
                                      {"dict_county":county_in, "dict_data":data_in})
              recs_list=result.fetchall()
              print recs_list
      
      if __name__=="__main__":
          ST=SQLiteTest()
      
          ## add some test records
          for ctr in range(5):
              ST.add_rec("Tennessee", "Davidson", "1")
          for ctr in range(3):
              ST.add_rec("Tennessee", "Daniels", "2")
          for ctr in range(3):
              ST.add_rec("Tennessee", "Davidson", "2")
      
          ## print all "Davidson"
          ST.lookup_county("Davidson")
      
          ## "Davidson" and "2"
          print "-"*50
          ST.lookup_data_and_county("Davidson", "2")

      Comment

      Working...