我有这两本字典:
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
然后我用defaultdict(lambda: defaultdict(dict))
计算gamma[worker][example]
。我首先使用一个函数来初始化值,然后使用另一个函数来更新值,但是在update函数中,计算要花很长时间才能完成,这就是为什么我想找到一种更快的方法来完成,将嵌套的dict转换为numpy数组。有人能给个提示吗
以下是迄今为止的代码:
#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
目前没有回答
相关问题 更多 >
编程相关推荐