0

I have this code:

import numpy as np

result = {}
result['depth'] = [1,1,1,2,2,2]
result['generation'] = [1,1,1,2,2,2]
result['dimension'] = [1,2,3,1,2,3]
result['data'] = [np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0])]

for v in np.unique(result['depth']):
    temp_v = np.where(result['depth'] ==  v)
    values_v = [result[string][temp_v] for string in result.keys()]
    this_v = dict(zip(result.keys(), values_v))

in which I want to create a new dictcalled 'this_v', with the same keys as the original dict result, but fewer values.

The line:

values_v = [result[string][temp_v] for string in result.keys()]

gives an error

TypeError: list indices must be integers, not tuple

which I don't understand, since I can create ex = result[result.keys()[0]][temp_v] just fine. It just does not let me do this with a for loop so that I can fill the list.

Any idea as to why it does not work?

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
SuperCiocia
  • 1,823
  • 6
  • 23
  • 40

1 Answers1

2

I'm not sure what you are trying to achieve but I could solve your issue:

np.where is returning a tuple, so to access you need to do give the index temp_v[0]. Also the value of the tuple is an array so to loop over the value you need to run another loop a for a in temp_v[0] which helps you you access the value.

import numpy as np

result = {}
result['depth'] = [1,1,1,2,2,2]
result['generation'] = [1,1,1,2,2,2]
result['dimension'] = [1,2,3,1,2,3]
result['data'] = [np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0])]

for v in np.unique(result['depth']):
    temp_v = np.where(result['depth'] ==  v)
    values_v = [result[string][a] for a in temp_v[0] for string in result.keys()]
    this_v = dict(zip(result.keys(), values_v))
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • Hey, thanks. I actually found a better way by using a mask. Now I have temp_v = (result['depth'] == v) values_v = [result[string][temp_v] for string in result.keys()] but it still gives an error, "only integer scalar arrays can be converted to a scalar index" – SuperCiocia Aug 23 '18 at 12:19
  • try this line `values_v = [result[string][a] for a in temp_v[0] for string in result.keys()]` – Arghya Saha Aug 23 '18 at 12:20
  • That works, but I'm trying to recude the number of for loops. Hence the masking idea – SuperCiocia Aug 23 '18 at 12:21
  • could you edit your code in the question, I can help you then – Arghya Saha Aug 23 '18 at 12:23
  • I asked it here: https://stackoverflow.com/questions/51985766/python-applying-a-mask-to-an-array-in-a-for-loop – SuperCiocia Aug 23 '18 at 12:25
  • https://stackoverflow.com/questions/46902367/numpy-array-typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-i check this answer. Thats premature optimisation. So I would recommend you to stick to the solution I suggested and move on unless anyone does a ground breaking revolution in this field. – Arghya Saha Aug 23 '18 at 12:29