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.]

3

Best Answer


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]

You can use numpy.repeat and pass i in your for loop:

import numpy as npa = 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.]

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)], [])