有效搜索嵌套列表

时间:2014-01-31 15:48:39

标签: python python-3.x

我有一个充满税务数据的csv文件。我将数据读入嵌套列表,因此格式如下:

['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']

我希望能够输入州和婚姻状况,然后返回相关的费率和括号列表。我在这里已经这样做了,但我觉得有一个更简单的方法。有什么建议吗?

search_state  = 'Alabama'
search_status = 'Single'
rates = []
brackets = []
for sublist in cleaned_data:
  if search_state in sublist[0] and search_status in sublist[1]:
    if 'rate' in sublist[1]:
      rates = [eval(x) for x in sublist[2:]]
    if 'bracket' in sublist[1]:
      brackets = [eval(x) for x in sublist[2:]]

1 个答案:

答案 0 :(得分:4)

使用嵌套字典可以提供更好的服务:

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']

假设您的cdv文件如下所示:

'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'

你可以这样构造嵌套的dict:

import csv

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

print(rates)       

打印:

{'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]}}

修改

正如评论中所指出的,三层嵌套字典可能更好,就像这个数据结构一样:

rates={'Alabama':{'Single': {'rate':['0.02', '0.04', '5'],
                             'bracket': ['500', '3000']},
                  'Couple': {'rate': ['0.02', '0.04', '0.05'],
                             'bracket': ['1000', '6000']}}}

虽然使用defaultdict或setdefault来处理带有缺失键的双层dict是微不足道的,但要优雅地处理多个级别需要更多一点。

我最喜欢的是使用类似autovivification子类的Perl这样的字典:

class AutoVivify(dict):
    """Implementation of perl's autovivification feature."""
    def __missing__(self, item):
        value = self[item] = type(self)()
        return value 

rates=AutoVivify()
with open(ur_file) as f:
    for line in csv.reader(f, skipinitialspace=True, quotechar="'"):
        state=line[0]
        k1,k2=line[1].split()
        rates[state][k1][k2]=[float(e) for e in line[2:]]

print(rates)  

打印:

{'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]}}}