简体   繁体   English

使用循环将numpy数组保存到字典中

[英]Saving numpy array into dictionary using loop

Below is my loop to loop through a bigger array (sortdata), pull out individual columns, and save those into a dictionary based on its iteration in the loop. 下面是我的循环,循环遍历更大的数组(sortdata),提取各个列,然后根据循环中的迭代将其保存到字典中。 My problem is that this loop is only looping through and saving just one column. 我的问题是,此循环仅循环并仅保存一列。 It saves the variabledict[1] array and nothing else. 它保存了variabledict [1]数组,仅此而已。 The sortdata array contains four columns (the first two do not have pertinent data so I omitted them in the code). sortdata数组包含四列(前两列没有相关数据,因此我在代码中省略了它们)。 There should be a variabledict[0]. 应该有一个变量dict [0]。 Any help would be greatly appreciated. 任何帮助将不胜感激。

datavalues = floating number that pertains to total columns datavalues =涉及总数列的浮点数

sortdata = large array I am pulling data from sortdata =大数组我正在从中提取数据

for k in range(int(datavalues - 2)):
  datavalloop = sortdata[:][0:,k + 2]
  variabledict = {}
  variabledict[k] = datavalloop

Place variabledict = {} outside loop. variabledict = {}放在循环外。 It is clearing dictionary values to Null on every iteration leaving only values of the last iteration. 它将在每次迭代中将字典值清除为Null ,仅保留最后一次迭代的值。

Place vaiabledict outside the loop. 将vaiabledict放在循环之外。 You are resetting it every time: 您每次都在重置它:

variabledict = {}
for k in range(int(datavalues - 2)):
    datavalloop = sortdata[:][0:,k + 2]
    variabledict[k] = datavalloop

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

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