Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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]
np.empty(8,4)
One easy way is to just use np.repeat:
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]])
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
np.empty(8,4)?