I have some data like this:
FeatureName,Machine,LicenseHost
Feature1,host1,lichost1
Feature1,host2,lichost1
Feature2,host1,lichost2
Feature1,host1,lichost1
and so on...
I want to maintain a nested dictionary where the first level of key is the feature name, next is machine name, finally license host, and the value is the number of times that combination occurs.
Something like:
dictionary['Feature1']['host1']['lichost1'] = 2
dictionary['Feature1']['host2']['lichost1'] = 1
dictionary['Feature2']['host1']['lichost2'] = 1
The obvious way of creating/updating such a dictionary is (assuming I am reading the data line by line from the 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
This ensures that I will never run into key not found errors at any level.
What is the best way to do the above (for any number of nested levels) ?