How to create a nested dictionary and add items to it via adding more arguments [python]

Question:

I was not sure how to phrase the question but what I am trying to do is to create a nested dictionary with corresponding names of a file where the value is a numpy file of the corresponding arguments. When more elements are added to the specific argument (so more elements in the task or score list), it should be added in the dictionary accordingly. Here is something I tried:

def mk_dict(task, score, datadir):
    for id, t in enumerate(task):
        for id, s in enumerate(score):
            all_f = glob.glob(opj(datadir, rf"{t}sub-**{s}.npy"))
            d = defaultdict(dict)
            d[t][s] = np.load(all_f[id])
            return d


dir = r"C:Users....."

tasks = ["high", "medium", "low"]
scores = ["precision", "accuracy", "f1"]

dic = mk_dict(tasks, scores, dir)

my output is: {"high": {"precision": array...}} taking only the first element of each list.

I had previously done something else and my output was: {"high": {"precision":array}, "high":{"accuracy":array} etc..}

my desired output is however:

{"high": {"precision":array, "accuracy":array, "f1":array}, "medium": {"precision":array, "accuracy":array, "f1":array}, "low".....etc....}}

I’ve been struggling with this for a while now. Any help is appreciated, thanks a lot!

Asked By: Merve

||

Answers:

Assuming the following things:

You have a directory named dir and it contains subfolders with names high, medium and low. Each of these subfolders contain sub-* folders.
For each sub-* folder, there are three files: precision.npy, accuracy.npy and f1.npy. All of these files contain one numpy array each.

If my assumptions are correct, the following code will take care of everything you want.

import glob
import os
import numpy as np

def mk_dict(task, score, datadir):
    d = {}
    for t in task:
        d[t] = {}
        for s in score:
            all_f = glob.glob(opj(datadir, rf"{t}sub-**{s}.npy"))
            d[t][s] = np.load(all_f[0])
    return d
Answered By: luke jon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.