【问题标题】:How to restrict the print output from a numpy array when computing an image wavelet?计算图像小波时如何限制numpy数组的打印输出?
【发布时间】:2013-06-23 10:13:00
【问题描述】:

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

【问题讨论】:

    标签: python printing numpy


    【解决方案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))
    

    【讨论】:

    • 我更喜欢for elem in arr.flat[:50]:
    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 2015-10-12
    • 2014-04-08
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多