I have a for loop which is pulling AWS snapshots, which then puts the snapshot in a dict
.
for snapshot in current_snapshots:
dict1 = {snapshotYear:{snapshotMonth:{snapshotDay:[[snapshot]]}}}
print dict1
This correctly prints out
{"2014": {"7": {"22": [["Snapshot:snap-XXXXXXXX"]]}}}
{"2014": {"7": {"22": [["Snapshot:snap-YYYYYYYY"]]}}}
I want to be able to say that if the snapshotDay
is the same, it should print out
{"2014": {"7": {"22": [["Snapshot:snap-XXXXXXXX"], ["Snapshot:snap-YYYYYYYY"]]}}}
, and if it's the same snapshotMonth
print out
{"2014": {"7": {"22": [["Snapshot:snap-XXXXXXXX"]}, {"15": [["Snapshot:snap-YYYYYY"]]}}}
This requires setting the result of the for loop equivalent. I'm not sure how to go about doing this.
from collections import defaultdict
defaultdict_dict=lambda :defaultdict(defaultdict_dict)
data = defaultdict(defaultdict_dict)
for snapshot in current_snapshots:
try:
data[snapshotYear][snapshotMonth][snapshotDay].append(snapshot)
except AttributeError:
data[snapshotYear][snapshotMonth][snapshotDay] = [snapshot]
print json.dumps(data)
is probably how I would handle it ...