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)
Answer from vallentin on Stack OverflowYou'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)
python - Append data in 2 dimensional array through for loop - Stack Overflow
Two dimensional array in python - Stack Overflow
python - Numpy append 2D array in for loop over rows - Stack Overflow
python - Initialize 2D array within loop - Stack Overflow
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]] ?
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']]
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]
Start with an empty list:
output_arr = []
Append within the loop:
for _ in range(5):
output_arr.append([1, 1])
Outside the loop, just call np.array:
X = np.array(output_arr)
If you really just want to construct with numpy, construct an np.empty array at the beginning, and then if you need to operate on the currently filled part of the array, use output[:i+1]
import numpy as np
output_arr = np.empty((5,2))
for i in range(0,4):
temp_arr = np.ones(2)
print temp_arr.shape
output_arr[i] = temp_arr
print output_arr[:i + 1].shape
print output_arr.shape
A simple list comprehension and zip will be sufficient, Use:
c = [[x + y for x, y in zip(s1, s2)] for s1, s2 in zip(a, b)]
Result:
#print(c)
[[6, 6, 9, 9], [9, 11, 12, 10]]
What you are seeking is essentially matrix addition:
import numpy as np
a = np.array([[5, 4, 5, 4], [4, 5, 6, 8]])
b = np.array([[1, 2, 4, 5], [5, 6, 6, 2]])
c = a + b
Where "array" is numpy's vector and matrix object, so when you return "c", you should see
>>> c
array ([[6, 6, 9, 9],
[9, 11, 12, 10]])
Very early into my newest hobby, learning to code so forgive my poor etiquette
Trying to figure out when iterating through a 2d list with a for loop
This structure doesn't preserve the 2d list
new_list = []
for temp_var1 in two_d_lst:
for temp_var2 in temp_var1:
new_list.append(temp_var2.method())But this structure does
new_list = []
for temp_var1 in two_d_lst:
unpacking_lst = []
for temp_var2 in temp_var1:
unpacking_lst.append(temp_var2.method())
new_list.append(unpacking_lst)I'm not sure I understand why...
Greatly appreciate sarcastic remarks, especially if it helps me understand this.