注意: 我找到了答案并回答了我自己的问题,但我必须等待 2 天才能接受我的答案。
如何使用除零以外的其他值初始化大小为 800 x 800 的 numpy 数组?:
array = numpy.zeros((800, 800))
Run Code Online (Sandbox Code Playgroud)
我正在寻找这样的解决方案,我可以在其中传递值列表(或列表)。
array = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
Run Code Online (Sandbox Code Playgroud)
编辑: 我不想用相同的值填充它。
我想要一个快速的代码示例来演示这是如何工作的,而无需像我在其他问题中找到的那样长的详细示例。我还想要一些简单易懂的东西,比如将 python 数组转换为 numpy 数组。我也在寻找二维数组的具体情况。
小智 7
您可以使用 fill 方法来初始化数组。
x = np.empty(shape=(800,800))
x.fill(1)
Run Code Online (Sandbox Code Playgroud)
我自己找到了答案: 这段代码做了我想要的,并表明我可以放置一个 python 数组(“a”)并将其变成一个 numpy 数组。对于将其绘制到窗口的代码,它将其颠倒绘制,这就是我添加最后一行代码的原因。
# generate grid
a = [ ]
allZeroes = []
allOnes = []
for i in range(0,800):
allZeroes.append(0)
allOnes.append(1)
# append 400 rows of 800 zeroes per row.
for i in range(0, 400):
a.append(allZeroes)
# append 400 rows of 800 ones per row.
for i in range(0,400):
a.append(allOnes)
#So this is a 2D 800 x 800 array of zeros on the top half, ones on the bottom half.
array = numpy.array(a)
# Need to flip the array so my other code that draws
# this array will draw it right-side up
array = numpy.flipud(array)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25669 次 |
| 最近记录: |