Python convert defaultdict(lambda: defaultdict(dict)) to numpy array

788 Views Asked by At

I have these two dictionaries:

alpha[worker] = {'A3M34FQ1OVNWPG': 1, 'AZH91RXTSG1NZ': 1, 'AHJGJ2J15SEHY': 1, 'A2IR6T0Y2MSDYD': 1, 'AGV7F8F0IV2MY': 1}
beta[example] = {'107_1108_0': 1, '953_1938_1': 1, '329_2157_0': 1, '411_1794_0': 1, '965_1633_0': 1

Then I calculate gamma[worker][example] with defaultdict(lambda: defaultdict(dict)). I first use a function that initiates the values and then another one that updates them, but in the update function, the calculations take forever to complete, and that's why I would like to find a faster way to do it, by converting the nested dict to a numpy array. Can anyone give a hint?

Here's the code so far:

#Initilization of alpha, beta, gamma

def Init_alpha_beta_gamma(self):
    alpha={}
    beta={}
    gamma=defaultdict(lambda: defaultdict(dict))


    for worker in self.w2el.keys():
        alpha[worker]=1
    for example in self.e2wl.keys():
        beta[example]=1
    for worker in self.w2el.keys():
        for example in self.e2wl.keys():
            gamma[worker][example]=1
    return alpha,beta,gamma
def Update_alpha_beta_gamma(self):
    #[a1, a2, ..., aN, ..., b1, b2, ..., bM,.., g11, g12, g13,..,gNM] are stored in x0
    x0=[]
    for worker in self.workers:
        x0.append(self.alpha[worker])
    for example in self.examples:
        x0.append(self.beta[example])
    for worker in self.workers:
        for example in self.examples:
        x0.append(self.gamma[worker][example]) #TODO: convert to numpy array
0

There are 0 best solutions below