简体   繁体   English

如何动态获取字典?

[英]How get a dictionary dynamically?

I have a for i in range(1,80): with the size 80 and there is a dictionary index = {} in my for loop.我有一个for i in range(1,80):大小为 80 并且我for循环中有一个字典index = {} I want for each time that my for loop circle, adds a key-value equal to i and type value equal to a list.我希望每次我for循环圈添加一个等于i的键值并键入等于一个列表的值。

I also tried this code:我也试过这段代码:

import numpy as np

 index  = {}
 for i in range(1, 80):
      tmp = L * msk
      (r, c) = np.nonzero(tmp > 0)
      v = np.size(c)  # v equal 24
      I = np.arange(v) # I = [0,1,2,3,...,23]
      Ind = np.empty((v, 2))
      Ind[i , :] = (r[I[i]], c[I[I]])
      index = {key = I: type = Ind} 

In the last line, you're changing the entire index reference to a new dictionary.在最后一行中,您将整个index引用更改为新字典。 You need to add a new key to index , not change it entirely.您需要向index添加一个新键,而不是完全更改它。 Try changing that line to:尝试将该行更改为:

index[i] = Ind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM