我有一些这样的数据:
FeatureName,Machine,LicenseHost
Feature1,host1,lichost1
Feature1,host2,lichost1
Feature2,host1,lichost2
Feature1,host1,lichost1
依旧......
我想维护一个嵌套字典,其中第一级密钥是功能名称,其次是计算机名称,最后是许可证主机,值是组合发生的次数。
类似的东西:
dictionary['Feature1']['host1']['lichost1'] = 2
dictionary['Feature1']['host2']['lichost1'] = 1
dictionary['Feature2']['host1']['lichost2'] = 1
创建/更新这样一个字典的显而易见的方法是(假设我从CSV中逐行读取数据):
for line in file:
feature, machine, license = line.split(',')
if feature not in dictionary:
dictionary[feature] = {}
if machine not in dictionary[feature]:
dictionary[feature][machine] = {}
if license not in dictionary[feature][machine]:
dictionary[feature][machine][license] = 1
else:
dictionary[feature][machine][license] += 1
这可以确保我永远不会遇到任何级别的密钥未找到错误。
执行上述操作的最佳方法是什么(对于任意数量的嵌套级别)?
答案 0 :(得分:2)
您可以使用defaultdict
:
from collections import defaultdict
import csv
def d1(): return defaultdict(int)
def d2(): return defaultdict(d1)
def d3(): return defaultdict(d2)
dictionary = d3()
with open('input.csv') as input_file:
next (input_file);
for line in csv.reader(input_file):
dictionary[line[0]][line[1]][line[2]] += 1
assert dictionary['Feature1']['host1']['lichost1'] == 2
assert dictionary['Feature1']['host2']['lichost1'] == 1
assert dictionary['Feature2']['host1']['lichost2'] == 1
assert dictionary['InvalidFeature']['host1']['lichost1'] == 0
如果多功能def
打扰你,你可以更简洁地说同样的话:
dictionary = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))