-1

I know there are functions for making arrays of zeros and ones, but I need to make an array of twos.

My ones array works:

np.ones((20), dtype=int).reshape(4, 5)

I just want to replace all the ones with twos in a short line of code.

1
  • 1
    just multiply by 2. It only adds 2 characters to your line of code. Or add 2 to an np.zeros Commented Feb 14, 2024 at 1:08

2 Answers 2

0

Try np.full() where you specify fill_value=2:

arr = np.full(20, 2, dtype=int).reshape(4, 5)
print(arr)

Prints:

[[2 2 2 2 2]
 [2 2 2 2 2]
 [2 2 2 2 2]
 [2 2 2 2 2]]
Sign up to request clarification or add additional context in comments.

2 Comments

Please, try looking for potential duplicates before you fire off an answer 3 minutes after the question was submitted. Is this really so novel that it warrants another answer added to the pile? Even the OP figured it out in 12 minutes.
@DanMašek, maybe he was writing this at the same time. The question is trivial enough to get multiple answers.
-1

Nevermind! I found it np.full((10), 2, dtype=float).reshape(2, 5)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.