It seems that this code wont run due to an 'str' object has no attribute 'append' error and according to the terminal the problems is due to the following line: new_arr[rows*i + j].append(arr[i][j])
I am trying to convert a matrix to a list.
Any help would be appreciated!
def two_d_translate(arr):
rows = len(arr)
cols = len(arr[0])
total_elements = rows * cols
new_arr = ['0']*total_elements
#the number of rows in a mat
for i in range(len(arr)):
for j in range(len(arr[0])):
new_arr[rows*i + j].append(arr[i][j])
return new_arr
new_arris a 1-dimensional array of strings, not a 2-dimensional array.new_arr[rows*i + j]is the string'0'. What are you expectingappend()to do?appending onto a string based on the error, so I would assume you're trying toappendto a'0'that is in the list. in fact,new_arris still a 1D array, so when you access it with one subscript, you're accessing a'0'directly instead of, say, a row of'0's.