Split Output To Populate Nested Dictionary Python
I am trying to populate a nested dictionary, but are having trouble since I am fairly new to python (also to Stack Overflow). I have a txt file with paths to photos and I would lik
Solution 1:
You can use nested sets of collections.defaultdict
to build your data:
from collections import defaultdict
dct = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
withopen('validation_labels.txt','r') as f:
for line in f:
line = line.strip('/photo/')
user, month, week, image = line.split('_')
dct[user][month][week].append(image)
Post a Comment for "Split Output To Populate Nested Dictionary Python"