By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,745 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,745 IT Pros & Developers. It's quick & easy.

Dealing with MULTIDIMENSIONAL dictionaries python

P: 1
Completely new to python and wondering how i can do this problem with multidimensional 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(called state_dict). Then i want state dict to take county name as key and to take another dictionary as value(county_dict). and then county_dict to take 4 keys of info and their values as their appropriate values.
what i have so far is
Expand|Select|Wrap|Line Numbers
  1. state=[]
  2. usa_dict = {}
  3. state_dict = {}
  4. county_dict = {}
  5. data = open("est10ALL.txt","rU")
  6. for line in data:
  7.   line=line.strip()
  8.   line=line.split()
  9.   state.append(line)
  10.      if line[-3] not in usa_dict:
  11.          usa_dict[line[-3]]= state_dict
  12.          if line[-5] not in state_dict:
  13.                state_dict[line[-5]] = county_dict
Mar 19 '12 #1
Share this Question
Share on Google+
2 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
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?
Expand|Select|Wrap|Line Numbers
  1. {"Tennessee": {"Davidson": {'key1': value1, 'key2': value2, 'key3': value3, 'key4': value4}}}
Mar 19 '12 #2

Expert 100+
P: 626
Generally, SQLite (comes with Python) works better than multi-dimensional dictionaries-->tutorial.
Expand|Select|Wrap|Line Numbers
  1. import sqlite3 as sqlite
  2.  
  3. class SQLiteTest():
  4.     def __init__(self):
  5.         # Create a connection to the (memory) database file
  6.         self.con = sqlite.connect(':memory:')
  7.  
  8.         # Get a Cursor object that operates in the context of Connection con
  9.         self.cur = self.con.cursor()
  10.  
  11.         self.cur.execute("CREATE TABLE test_it (state varchar, county varchar, data varchar)")
  12.  
  13.     ##----------------------------------------------------------------------
  14.     def add_rec(self, state_in, county_in, add_var):
  15.         self.cur.execute("insert into test_it values (?, ?, ?)", (state_in, county_in, add_var))
  16.         self.con.commit()
  17.  
  18.     def lookup_county(self, county_in):
  19.         ## lookup using a tuple
  20.         result=self.cur.execute('SELECT * FROM test_it where county=?', (county_in,))
  21.         recs_list=result.fetchall()
  22.         print recs_list
  23.  
  24.     def lookup_data_and_county(self, county_in, data_in):
  25.         ## lookup using a dictionary
  26.         result=self.cur.execute('SELECT * FROM test_it where county=:dict_county and data=:dict_data', \
  27.                                 {"dict_county":county_in, "dict_data":data_in})
  28.         recs_list=result.fetchall()
  29.         print recs_list
  30.  
  31. if __name__=="__main__":
  32.     ST=SQLiteTest()
  33.  
  34.     ## add some test records
  35.     for ctr in range(5):
  36.         ST.add_rec("Tennessee", "Davidson", "1")
  37.     for ctr in range(3):
  38.         ST.add_rec("Tennessee", "Daniels", "2")
  39.     for ctr in range(3):
  40.         ST.add_rec("Tennessee", "Davidson", "2")
  41.  
  42.     ## print all "Davidson"
  43.     ST.lookup_county("Davidson")
  44.  
  45.     ## "Davidson" and "2"
  46.     print "-"*50
  47.     ST.lookup_data_and_county("Davidson", "2")  
Mar 21 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.