2
array = np.empty(8,4)

for I in range(8):
   array[I] = I 

Can this be implemented without for loop. I would like know other approaches

array = np.empty(8,4)
for I in range(8):
   array[I] = I 

[0,0,0,0]
[1,1,1,1]
    .
    .
    .
[7,7,7,7]
2
  • What are you trying to create with np.empty(8,4)? Commented Jan 29, 2019 at 17:33
  • creating 8 rows and 4 columns 2 dimensional array and assigns 0 for every cell.Syntax typo: np.empty((8,4)) Commented Jan 29, 2019 at 17:37

1 Answer 1

1

One easy way is to just use np.repeat:

array = np.repeat(np.arange(8), 4).reshape(8, 4)

array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]])
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.