Python 如何构造numpy数组以显示方形方案

Python 如何构造numpy数组以显示方形方案,python,python-3.x,numpy,Python,Python 3.x,Numpy,我想询问是否可以使用numpy创建此方案: 1 2 3 1 1 2 2 3 3 1 2 3 。。。然后使用命令,我将在这些数字的帮助下,将“0”或“*”添加到空列中,但需要在角落中有空列。如果可能的话,我应该如何编写代码?将@LightVillet的答案调整为不那么复杂: import numpy as np n = 3 #number of elements (3 in your example) a = np.array([[0] * (n +

我想询问是否可以使用
numpy
创建此方案:

  1 2 3
1       1
2       2
3       3
  1 2 3

。。。然后使用命令,我将在这些数字的帮助下,将“0”或“*”添加到空列中,但需要在角落中有空列。如果可能的话,我应该如何编写代码?

将@LightVillet的答案调整为不那么复杂:

import numpy as np
n = 3 #number of elements (3 in your example)
a = np.array([[0] * (n + 2)] * (n + 2)) #creating zero-fill 2-d array
for i in range(len(a)):
    for j in range(len(a[i])):
        if i == 0 or i == len(a) - 1: #if we are on a border of array
            if j != 0 and j != len(a[i]) - 1: #but not in corners
                a[i][j] = j
        if j == 0 or j == len(a[i]) - 1:# if we are on a border of array
            if i != 0 and i != len(a) - 1: #but not in corners
                a[i][j] = i
print(a)
import numpy as np
n = 3 #number of elements (3 in your example)
a = np.full((n+2, n+2), ' ') #creating ' '-fill 2-d array

for i in range(1,n+1):
    a[0,i] = i    #top edge
    a[n+2,i] = i  #bottom edge
    a[i,0] = i    #left edge
    a[i,n+2] = i  #right edge

print(a)

复杂性:
O(n)

更复杂、效率更低的方法

import numpy as np
size = int(input("Please enter size of the Matrix : "))
empty = np.zeros([size,size])
numberCounter = 0
rowNumberCounter = 0
for rowCounter in range(size):
    empty[rowCounter][size-1] = 0
    for colCounter in range(size):
        if rowCounter == 0 and colCounter == 0:
            empty[rowCounter][colCounter] = 0
        if rowCounter == 0 and colCounter < size-1:
            empty[rowCounter][colCounter] = numberCounter
            numberCounter = numberCounter + 1
        if rowCounter == size-1 and colCounter != 0 and colCounter <size-1:
            numberCounter = numberCounter + 1
            empty[rowCounter][colCounter] = numberCounter

    if rowCounter !=0 and rowCounter < size-1:
        empty[rowCounter][0] = rowNumberCounter + 1
        empty[rowCounter][size-1] = rowNumberCounter + 1
        rowNumberCounter = rowNumberCounter + 1
    numberCounter = 0

print(empty)

pushButton = input('Push * to convert 0 to * :')
if pushButton == '*':
        empty_str = empty.astype(str)
        for rowCounter in range(size):
            for colCounter in range(size):
                if empty_str[rowCounter][colCounter] == '0.0':
                    empty_str[rowCounter][colCounter] = '*'

print(empty_str)

要以高效的方式使用数组和无循环构造数组,可以使用:

import numpy as np

a = np.full((5, 5), ' ')
v = np.array((' ', '1', '2', '3', ' '))

a[0, :] = v
a[-1, :] = v
a[:, 0] = v
a[:, -1] = v
打印输出:

>>> for i in a:
>>>     print(' '.join(i))

  1 2 3  
1       1
2       2
3       3
  1 2 3  

你试过/研究过什么?除非数组的数据类型是
str
(或者
使用python@adirabargil,一切都是可能的),否则我不明白numpy是如何工作的,这就是问题所在。我只能创建np.zero((3,3))这会输出零和我想要的任何东西。在google eitherTry中找不到任何东西来硬编码所需输出的示例…@S3DEV根据我的研究,我发现不可能完全按照我想要的方式创建方案。我很感兴趣这是否可能…而且非常不符合Python,如果我可以诚实的话。别担心,ju圣烤我:),每天学习。我会考虑你的批评。看起来您来自.NET或JS背景。不用担心,我们每天都在学习。坚持下去。(要实现这一点,请使用
numpy
方法。)很好。看看
np.full((5,5),“”)
>>> for i in a:
>>>     print(' '.join(i))

  1 2 3  
1       1
2       2
3       3
  1 2 3