2013-04-21 55 views
2

我想使用numpy.where中的索引值來打印該索引的字符串內容。我試圖通過遍歷數組從0到數組的長度來做到這一點。但長度總是1 ..?爲什麼我的數組長度爲1時,用numpy.where構建它?

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] 

此打印僅一個的值,但是在陣列實際上具有10

回答

2

numpy.where總是返回陣列的元組,即使當參數是1-d。它爲每個維度返回一個數組。

例如:

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

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

注意Out[3]示出長度1。在元組的單個對象的元組是指數的numpy的陣列。爲了得到陣列,剛拉出來的元組:

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

爲您的代碼,改變你的missingValue測算到

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

大,這使得更多的意義,現在,我的代碼工作。非常感謝。 – user2177088 2013-04-21 03:33:06

相關問題