say i have array [[1,2,3,4,5]], how can i duplicate it 5 times to [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] ?
Videos
You're forgetting to append the empty list beforehand. Thus why you get a, IndexError when you try to do arr[i].
arr = []
for i in range(10):
arr.append([])
for j in range(5):
arr[i].append(i*j)
You need to define your initial array in the following way: arr=[[] for i in range(10)], as you cannot append a value to a nonexistent array (which is what happens when i>=1). So the code should look like:
arr=[[] for i in range(10)]
for i in range(10):
for j in range(5):
arr[i].append(i*j)
print(i,i*j)
print(arr)
Use np.empty to initialize an empty array and define the axis you want to append across:
import numpy as np
mat = np.empty((0,2))
for i in np.arange(3):
val = np.random.rand(2, 2)
mat = np.append(mat,val, axis=0)
print(mat)
Output:
[[0.08527627 0.40567273]
[0.39701354 0.72642426]
[0.17540761 0.02579183]
[0.76271521 0.83032347]
[0.08105248 0.67986726]
[0.48079453 0.37454798]]
However, as stated in my comment, if you need to append a lot of times you should look into initializing an array of the correct size then assigning values over using np.append() or appending to a list instead (if you do not know the size of the array) and then creating a numpy array after
Another way to do it is using the * operator. Notice you can do this even if you are not working with Numpy
mat = []
for i in np.arange(3):
if len(mat) == 0:
mat = np.random.rand(2, 2)
else:
mat = [*mat, *np.random.rand(2, 2)]
# And in case you need it back as a 2D list and not numpy arrays
mat = [list(element) for element in mat]
You do not "declare" arrays or anything else in python. You simply assign to a (new) variable. If you want a multidimensional array, simply add a new array as an array element.
arr = []
arr.append([])
arr[0].append('aa1')
arr[0].append('aa2')
or
arr = []
arr.append(['aa1', 'aa2'])
There aren't multidimensional arrays as such in Python, what you have is a list containing other lists.
>>> arr = [[]]
>>> len(arr)
1
What you have done is declare a list containing a single list. So arr[0] contains a list but arr[1] is not defined.
You can define a list containing two lists as follows:
arr = [[],[]]
Or to define a longer list you could use:
>>> arr = [[] for _ in range(5)]
>>> arr
[[], [], [], [], []]
What you shouldn't do is this:
arr = [[]] * 3
As this puts the same list in all three places in the container list:
>>> arr[0].append('test')
>>> arr
[['test'], ['test'], ['test']]
You can try this:
import numpy as np
listvals = []
for i in range(10):
listvals.append((xc, yc))
mat_vals = np.vstack(listvals)
This will output an ndarray like this:
[[ 2 0]
[ 3 1]
[ 4 2]
[ 5 3]
[ 6 4]
[ 7 5]
[ 8 6]
[ 9 7]
[10 8]
[11 9]]
Or this maybe is better:
import numpy as np
list_xc = []
list_yc = []
for i in np.arange(10):
list_xc.append(xc)
list_yc.append(yc)
mat_xc = np.asarray(list_xc)
mat_yc = np.asarray(list_yc)
mat_f = np.column_stack((mat_xc, mat_yc))
You can do this:
aList = []
for i in range(10):
aList.append([xc, yc])