python - Efficiently Searching Nested Lists -


i have csv file full of tax data. read data nested lists, formatted this:

['alabama', 'single rate', '0.02', '0.04', '5'] ['alabama', 'single bracket', '500', '3000'] ['alabama', 'couple rate', '0.02', '0.04', '0.05'] ['alabama', 'couple bracket', '1000', '6000'] 

i able input state , marital status , return relevant lists of rates , brackets. i've done here, feel there much simpler approach. suggestions?

search_state  = 'alabama' search_status = 'single' rates = [] brackets = [] sublist in cleaned_data:   if search_state in sublist[0] , search_status in sublist[1]:     if 'rate' in sublist[1]:       rates = [eval(x) x in sublist[2:]]     if 'bracket' in sublist[1]:       brackets = [eval(x) x in sublist[2:]] 

you better served nested dictionary:

rates={'alabama':{'single rate':['0.02', '0.04', '5'],                   'single bracket': ['500', '3000'],                   'couple rate': ['0.02', '0.04', '0.05'],                   'couple bracket': ['1000', '6000']}}  print(rates['alabama']['couple rate']) # ['0.02', '0.04', '0.05'] 

assuming cdv file looks this:

'alabama', 'single rate', '0.02', '0.04', '5' 'alabama', 'single bracket', '500', '3000' 'alabama', 'couple rate', '0.02', '0.04', '0.05' 'alabama', 'couple bracket', '1000', '6000' 

you can construct nested dict way:

import csv  rates={} open(ur_file) f:     line in csv.reader(f, skipinitialspace=true, quotechar="'"):         rates.setdefault(line[0],{})[line[1]]=[float(e) e in line[2:]]  print(rates)        

prints:

{'alabama': {'couple rate': [0.02, 0.04, 0.05],   'single rate': [0.02, 0.04, 5.0],   'single bracket': [500.0, 3000.0],   'couple bracket': [1000.0, 6000.0]}} 

edit

as pointed out in comments, 3 tier nested dict better, data structure:

rates={'alabama':{'single': {'rate':['0.02', '0.04', '5'],                              'bracket': ['500', '3000']},                   'couple': {'rate': ['0.02', '0.04', '0.05'],                              'bracket': ['1000', '6000']}}} 

while trivial use defaultdict or setdefault deal 2 tier dict missing keys, takes little more though deal multiple levels elegantly.

my favorite use perl autovivification subclass dict so:

class autovivify(dict):     """implementation of perl's autovivification feature."""     def __missing__(self, item):         value = self[item] = type(self)()         return value   rates=autovivify() open(ur_file) f:     line in csv.reader(f, skipinitialspace=true, quotechar="'"):         state=line[0]         k1,k2=line[1].split()         rates[state][k1][k2]=[float(e) e in line[2:]]  print(rates)   

prints:

{'alabama': {'single': {                           'rate': [0.02, 0.04, 5.0],                           'bracket': [500.0, 3000.0]},               'couple': {                            'rate': [0.02, 0.04, 0.05],                           'bracket': [1000.0, 6000.0]}}} 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -