Python中的3x1 numpy数组列表
我正在尝试编写一个python函数,该函数将接受RGB值的1D数组,并列出表示像素的3x1数组。这是我的功能Python中的3x1 numpy数组列表,python,arrays,list,numpy,multidimensional-array,Python,Arrays,List,Numpy,Multidimensional Array,我正在尝试编写一个python函数,该函数将接受RGB值的1D数组,并列出表示像素的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
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值的1D数组长度相同,但列表中的所有元素都是最后一个RGB值的3x1数组。还有,有没有一种方法我可以写这个,这样我就不需要那个if语句了?我之所以需要它,是因为x达到rgbLength,即使python中的for循环是包含的、独占的您可能对为您执行此任务的循环感兴趣 例如:
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]]
我只是想说明为什么你的代码不起作用@peter de Rivaz的答案应该是你在numpy应该做什么
当您添加allPixels.appendpixel时,您添加的是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
我想你可以使用-1,例如A.reforme-1,3。谢谢,我在课程中增加了这个改进,这给了你一个二维数组,而不是一维数组的列表。但a这可能是您真正想要的,b若不是,listB会简单地给您一个1D行的列表。所以,无论哪种方式,这都是一条路。在重塑中使用负值的效果是什么?它从数组的长度和其他参数推断出维度。这对我来说是新的:我在看到DSM的建议后才查阅了这个
>>> 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
>>> 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