0

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. My problem is that this loop is only looping through and saving just one column. It saves the variabledict[1] array and nothing else. The sortdata array contains four columns (the first two do not have pertinent data so I omitted them in the code). There should be a variabledict[0]. Any help would be greatly appreciated.

datavalues = floating number that pertains to total columns

sortdata = large array I am pulling data from

for k in range(int(datavalues - 2)):
  datavalloop = sortdata[:][0:,k + 2]
  variabledict = {}
  variabledict[k] = datavalloop
1
  • You are resetting variabledict to be an empty dict inside the loop, so it goes back to {} every time, and only the final iteration will leave anything stored in the dict. Commented Nov 7, 2014 at 16:15

2 Answers 2

1

Place variabledict = {} outside loop. It is clearing dictionary values to Null on every iteration leaving only values of the last iteration.

Sign up to request clarification or add additional context in comments.

Comments

1

Place vaiabledict outside the loop. You are resetting it every time:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.