2

Here's an example of what I want to do. I want to take b and add this to my array but in a particular format (again I just want to know the steps to do this, my code deals with strings):

import numpy as np

a = np.zeros(25, dtype= np.character).reshape(5,5)
b = [1,2,3,4,5,6,7,8,9]

for i in b:

    a[1:4,1:4] = i

print a

Output:

[['' '' '' '' '']
 ['' '9' '9' '9' '']
 ['' '9' '9' '9' '']
 ['' '9' '9' '9' '']
 ['' '' '' '' '']]

But what I want is this:

[['' '' '' '' '']
 ['' '1' '2' '3' '']
 ['' '4' '5' '6' '']
 ['' '7' '8' '9' '']
 ['' '' '' '' '']]

Could anyone give me an idea on how to do this? Thank you.

1 Answer 1

2
import numpy as np

a = np.zeros(25, dtype= np.character).reshape(5,5)
b = np.array([1,2,3,4,5,6,7,8,9])
a[1:4,1:4] = b.reshape(3,3)
print(a)

yields

[['' '' '' '' '']
 ['' '1' '2' '3' '']
 ['' '4' '5' '6' '']
 ['' '7' '8' '9' '']
 ['' '' '' '' '']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.