Aug-12-2019, 07:33 PM
it can probably be done with NumPy too
but there is defaultdict from collections module
but there is defaultdict from collections module
from collections import defaultdict import json spam = defaultdict(list) files = {'fle1.txt': 'John', 'file2.txt': 'James', 'file3.txt': 'John'} for fname, owner in files.items(): spam[owner].append(fname) print(spam) # dump as json eggs = json.dumps(spam) print(eggs)
Output:defaultdict(<class 'list'>, {'John': ['fle1.txt', 'file3.txt'], 'James': ['file2.txt']})
{"John": ["fle1.txt", "file3.txt"], "James": ["file2.txt"]}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs