2013-06-23 49 views
1

此代碼從圖像小波到控制檯打印出一串無窮無盡的數字。我需要限制輸出到前50或100個項目。我試圖做到這一點,但沒有得到我所需要的。如何在計算圖像小波時限制numpy陣列的打印輸出?

def waveletdbbiorone(self):  #function for Wavelets computation 
      for filename in glob.iglob ('*.tif'): 
      imgwbior = mahotas.imread (filename) #read the image 
      arraywbior = numpy.array([imgwbior])#make an array 
      coefwbior = pywt.wavedec(arraywbior,'db1')#compute wavelet coefficients 
      arr = numpy.array([coefwbior]) 
      np.set_printoptions(threshold=3) 
      # print arr 
      for elem in arr.flat: 
       print('{}\t'.format(elem)) #, end ='') #print out the result 

回答

1

嘗試這樣:

for i in range(min(50, len(arr.flat))): 
    elem = arr.flat[i] 
    print('{}\t'.format(elem)) 

可以縮短爲:

for i in range(min(50, len(arr.flat))): 
    print('{}\t'.format(arr.flat[i])) 


編輯:

或者,由Jaime,將更多Python的建議:

for elem in arr.flat[:50]: 
    print('{}\t'.format(elem)) 
+2

我更傾向於這樣的ELEM在arr.flat [50]:' – Jaime