Why is my array length 1 when building it with numpy.where?

arrays, numpy, python

Solution

`numpy.where` always returns a tuple of arrays, even when the argument is 1-D. It returns one array for each dimension.

For example:

In [2]: a = np.array([10, 5, 3, 9, 1])

In [3]: np.where(a > 5)
Out[3]: (array([0, 3]),)

Note that `Out[3]` shows a tuple of length 1. The single object in the tuple is the numpy array of indices. To get the array, just pull it out of the tuple:

In [4]: np.where(a > 5)[0]
Out[4]: array([0, 3])

For your code, change your calcuation of `missingValue` to

missingValue = np.where(checkValue == False)[0]

Problem

I want to use the index values from numpy.where to print the string contents of that index. I am trying to do this by iterating over the array from 0 to the length of the array. But the length is always 1..? ``` import numpy as np v,w,x, y, z = np.loadtxt('test.txt', dtype=str, delimiter='|', skiprows=2,usecols=(0,1,2,3,4), unpack=True) a,b,c, d, e = np.loadtxt('test2.txt', dtype=str, delimiter='|', skiprows=2,usecols=(0,1,2,3,4), unpack=True) checkValue = np.in1d(a, v) missingValue=(np.where(checkValue==False)) print len(missingValue) for i in range (len(MissingValue)): print a[i] ``` This prints just one value, but the array actually has 10

Original source