【问题标题】:List of 3x1 numpy arrays in PythonPython 中的 3x1 numpy 数组列表
【发布时间】:2013-12-13 15:04:43
【问题描述】:

我正在尝试编写一个 python 函数,该函数将采用 RGB 值的一维数组并制作一个代表像素的 3x1 数组列表。这是我的功能

def RGBtoLMS(rgbValues, rgbLength): #Passing in a list of rgbValues and an int representing the length of that list
    pixel = numpy.empty((3, 1), int)
    allPixels = list()
    x = 0
    for h in xrange(rgbLength):
        for i in xrange(len(pixel)):
            for j in xrange(len(pixel[i])):
                if x < rgbLength: #For some reason, x reaches rgbLength...
                    pixel[i][j] = rgbValues[x]
                    x+=1
        allPixels.append(pixel)
        for pixel in allPixels:
            print pixel

当我运行这段代码时,我注意到在我的循环的每次迭代中,它不是将每个像素添加到列表的末尾作为表示它的 3x1 数组,而是将列表中的所有值替换为最新的像素。最终结果是一个列表,只要我的一维数组的长度包含原始 RGB 值,但列表中的所有元素都是最后一个 RGB 值的 3x1 数组。另外,有没有办法可以写这个,所以我不需要那个 if 语句?我只需要它,因为 x 达到 rgbLength,即使 python 中的 for 循环是(包含,不包含)

【问题讨论】:

    标签: python arrays list numpy multidimensional-array


    【解决方案1】:

    您可能对为您执行此任务的reshape function 感兴趣。

    例如:

    import numpy
    A = numpy.array( [1,2,3,101,102,103,4,5,6,104,105,106] )
    B = A.reshape( (-1,3) )
    print B
    

    打印:

    [[  1   2   3]
     [101 102 103]
     [  4   5   6]
     [104 105 106]]
    

    【讨论】:

    • 我认为你可以使用-1,例如A.reshape(-1, 3).
    • 谢谢,我已经添加了这个改进
    • 当然这会给你一个二维数组而不是一维数组的列表。但是(a)这可能是您真正想要的,并且(b)如果不是,list(B) 会给您一个一维行的列表。所以,无论哪种方式,这都是要走的路。
    • reshape中使用负值有什么影响?
    • 它从数组的长度和其余参数推断维度(这对我来说是新的:在看到 DSM 的建议后,我刚刚查看了 here)
    【解决方案2】:

    我只是要说明为什么您的代码不起作用。 @peter de Rivaz 的回答应该是你在 numpy 中应该做的事情

    当您allPixels.append(pixel) 时,您附加了pixel 的发生率。如果稍后更改pixel 的值,则pixel 的所有发生率都会更改。看这个例子:

    >>> def RGBtoLMS(rgbValues, rgbLength): #Passing in a list of rgbValues and an int representing the length of that list
        pixel = numpy.empty((3, 1), int)
        allPixels = list()
        x = 0
        for h in xrange(rgbLength/3):
            for i in xrange(len(pixel)):
                for j in xrange(len(pixel[i])):
                    if x < rgbLength: #For some reason, x reaches rgbLength...
                        pixel[i][j] = rgbValues[x]
                        x+=1
            allPixels.append(pixel)
        for pixel in allPixels:
            print pixel, id(pixel)
    
    
    >>> RGBtoLMS([1,2,3,101,102,103,4,5,6,104,105,106],12)
    [[104]
     [105]
     [106]] 38693552
    [[104]
     [105]
     [106]] 38693552
    [[104]
     [105]
     [106]] 38693552
    [[104]
     [105]
     [106]] 38693552
    

    所以你可以做一些事情来获得预期的行为:

    >>> def RGBtoLMS(rgbValues, rgbLength): #Passing in a list of rgbValues and an int representing the length of that list
        pixel = numpy.empty((3, 1), int)
        allPixels = list()
        x = 0
        for h in xrange(rgbLength/3):
            for i in xrange(len(pixel)):
                for j in xrange(len(pixel[i])):
                    if x < rgbLength: #For some reason, x reaches rgbLength...
                        pixel[i][j] = rgbValues[x]
                        x+=1
            allPixels.append(pixel*1)
        for pixel in allPixels:
            print pixel, id(pixel)
    
    
    >>> RGBtoLMS([1,2,3,101,102,103,4,5,6,104,105,106],12)
    [[1]
     [2]
     [3]] 38301608
    [[101]
     [102]
     [103]] 37756712
    [[4]
     [5]
     [6]] 37949360
    [[104]
     [105]
     [106]] 38399424
    

    或者将pixel的定义移到循环中。

    >>> def RGBtoLMS(rgbValues, rgbLength): #Passing in a list of rgbValues and an int representing the length of that list
        allPixels = list()
        x = 0
        for h in xrange(rgbLength):
            if x < rgbLength: #For some reason, x reaches rgbLength...
                pixel = numpy.empty((3, 1), int)
                for i in xrange(len(pixel)):
                    for j in xrange(len(pixel[i])):
                        pixel[i][j] = rgbValues[x]
                        x+=1
                allPixels.append(pixel)
            else:
            break
        for pixel in allPixels:
            print pixel, id(pixel)
    
    
    >>> RGBtoLMS([1,2,3,101,102,103,4,5,6,104,105,106],12)
    [[1]
     [2]
     [3]] 38301608
    [[101]
     [102]
     [103]] 37756712
    [[4]
     [5]
     [6]] 37949360
    [[104]
     [105]
     [106]] 38399424
    

    【讨论】:

      猜你喜欢
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2019-10-11
      • 2023-02-05
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多