1

I want to ask whether it is possible to create with numpy this scheme:

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

... and then with commands I will add '0' or '*' to empty columns with the help of those numbers,but it is needed to have empty columns in corners. If it is possible how should I write the code?

S3DEV
  • 8,768
  • 3
  • 31
  • 42
Narmin
  • 69
  • 1
  • 8
  • What have you tried/researched? Unless the dtype of the array is `str` (or ` – S3DEV Jan 03 '21 at 20:00
  • 1
    Everything is possible with python , just give examples and describe yourself well, and show what have you tried, and work hard to understand, and do your best , :) – adir abargil Jan 03 '21 at 20:01
  • @adirabargil I dont quiet understand how numpy works and that is the problem.I only can create np.zeros((3, 3)) and that outputs zeros and nothing as I want.Can't find anything in google either – Narmin Jan 03 '21 at 20:08
  • Try to hardcode the examples of desired output... – adir abargil Jan 03 '21 at 20:09
  • @S3DEV from what I have researched I came to find it is impossible to create the scheme exactly the way I want.And I am interested whether it is even possible – Narmin Jan 03 '21 at 20:11
  • This could be helpful for the printing part https://stackoverflow.com/questions/9360103/how-to-print-a-numpy-array-without-brackets – Or b Jan 03 '21 at 20:17
  • Is your focus on a printed output, or on some array that can be used for calculation? `numpy` is good for calculations, not for fine tuned display. – hpaulj Jan 03 '21 at 20:36

4 Answers4

1
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)
LightVillet
  • 119
  • 3
1

To construct the array in an efficient manner, using array indexing and no loops, you can use:

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

Printing the output:

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

  1 2 3  
1       1
2       2
3       3
  1 2 3  
S3DEV
  • 8,768
  • 3
  • 31
  • 42
0

Adapted @LightVillet's answer to be less complex:

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)

Complexity: O(n)

wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
0

More complex and inefficient way,

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)

Output;

Please enter size of the Matrix : 5
[[0. 1. 2. 3. 0.]
 [1. 0. 0. 0. 1.]
 [2. 0. 0. 0. 2.]
 [3. 0. 0. 0. 3.]
 [0. 1. 2. 3. 0.]]
Push * to convert 0 to * :*
[['*' '1.0' '2.0' '3.0' '*']
 ['1.0' '*' '*' '*' '1.0']
 ['2.0' '*' '*' '*' '2.0']
 ['3.0' '*' '*' '*' '3.0']
 ['*' '1.0' '2.0' '3.0' '*']]
Nott
  • 303
  • 1
  • 8
  • 1
    ... and very un-pythonic, if I may be honest. – S3DEV Jan 03 '21 at 20:36
  • Don't worry, just roast me :), learning every day. I will consider your criticism. – Nott Jan 03 '21 at 20:41
  • Ha. Looks like you come from a .NET or JS background. No worries, we all learn everyday. Keep it up. ([Here is just one way](https://stackoverflow.com/a/65555926/6340496) to accomplish this, using a `numpy` approach.) – S3DEV Jan 03 '21 at 23:22