如何在计算图像小波时限制numpy数组的打印输出?

时间:2013-06-23 10:13:01

标签: python printing numpy

此代码打印出从图像小波到控制台的无穷无尽的数字列。我需要将输出限制为前50或100项。我试图做到这一点,但无法得到我需要的东西。

  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 个答案:

答案 0 :(得分: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所建议的那样,更多的pythonic:

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