1

此代码从图像小波打印出无穷无尽的数字列到控制台。我需要将输出限制为前 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 
4

1 回答 1

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))
于 2013-06-23T10:31:52.397 回答