Reputation: 53
I need to create a vector composed of numbers from 1 to 100 and each number is repeated 100 times. I was able to come up with this solution, but I need to avoid using i,i,i,i,i,i....,i,i,i
a = np.zeros(0)
for i in range(1,100):
a = np.r_[a,[i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i]]
print(a)
*Here is the output : [ 1. 1. 1. ... 99. 99. 99.]
Upvotes: 2
Views: 3857
Reputation: 86433
You can do it in one line with np.repeat
:
a = np.repeat(np.arange(1, 100), 100)
print(a)
# [ 1, 1, 1, ..., 99, 99, 99]
Upvotes: 5
Reputation: 16683
You can use numpy.repeat
and pass i
in your for loop
:
import numpy as np
a = np.zeros(0)
for i in range(1,100):
a = np.r_[a,np.repeat(i, 100)]
print(a)
[ 1. 1. 1. ... 99. 99. 99.]
Upvotes: 1
Reputation: 1068
You can do it with list comprehension (then cast it to numpy.array
if needed).
a = sum([[i] * 100 for i in range(1, 100)], [])
Upvotes: 1