-10

This is my data:

2018-06-19 136768197 e230702d
2018-06-19 136768197 f14c4f07
2018-06-15 1157040118 a527b130
2018-06-15 1157040118 f05a746f

As output I need the following dict

{
  '2018-06-19': {
    '136768197': ['e230702d', 'f14c4f07']
  },
  '2018-06-15': {
    '1157040118': ['a527b130', 'f05a746f']
  }
}

My attempt to resolve the problem:

d = defaultdict(list)
c = dict()
with open("c:/Python/hitId.txt") as f:    
    for line in f:
        key, s, h = line.split()
        d[s].append(h)
        c[key] = d

But the output contains duplicated data (formatted to highlight issue):

{'2018-06-15': {'1157040118': ['a527b130', 'f05a746f'],'136768197': ['e230702d', 'f14c4f07']}),
 '2018-06-19': {'1157040118': ['a527b130', 'f05a746f'],'136768197': ['e230702d', 'f14c4f07']})}
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40

1 Answers1

1

I think you want a default dict of default dicts as answered in Python: defaultdict of defaultdict? ; I debated whether this should be a duplicate question (and if someone wants to say it is, that's fine), but basically I think something like:

x = defaultdict(lambda: defaultdict(list))
with open("c:/Python/hitId.txt") as f:    
    for line in f:
        key, s, h = line.split()
        x[key][s].append(h)

should get you what you're looking for (a little extra work required to convert to a raw dict). If you want to ultimately just print it out

  import json
  print(json.dumps(x)) 

should do it

Foon
  • 6,148
  • 11
  • 40
  • 42