Mirroring rows in a matrix using loops / recursion?

For some matrix, I need to reflect all the rows in the matrix. for instance

[[2, 1], [4, 3]] 

will become

 [[1, 2], [3, 4]] 

I managed to do this for a (2 x 2) -case. But I'm having trouble reflecting something like this:

 [[1, 2, 3, 4], [1, 2, 3, 4]] 

It should become

 [[4, 3, 2, 1], [4, 3, 2, 1]] 

I want to do this with loops / recursion. If I use recursion, I would probably have a basic step, first to first interchange the internal elements, and then from here we would make a large matrix, including external elements and their replacement. However, I am having problems with the recursion step. After I swapped most of the elements, I want to include most of the elements inside the matrix, as well as swap them and continue until we reach the outer elements. How can this be implemented in code? This is what I have done so far:

 matrix = [[1, 2, 3, 4], [1, 2, 3, 4]] def mirror(matrix): # This corresponds to the basic step. The two inner most elements get swapped. if len(matrix) == 2: for i in range(len(matrix)): for j in range(len(matrix)): # Store one element in a temporal variable temp = matrix[i][j] matrix[i][j] = matrix[i][len(matrix) - 1] matrix[i][len(matrix)-1] = temp return matrix else: # Recursion step for i in range(len(matrix)): for j in range(len(matrix)): return (matrix + mirror(matrix[(len(matrix) // 2) - 1 : len(matrix)])) 

The recursive step is wrong, I think. I tried using the slice operator, but I'm not sure how this should be done correctly. Any help with this problem would be appreciated.

+5
source share
3 answers

The recursive solution is pretty trivial, just recursing through an array modifying each subarray:

 arr= [[2, 1], [4, 3]] def reve(l): # if we have recursed across all sub arrays just return empty list if not l: return [] # else reverse the first current sublist l[0] and recurse on the remaining sublists return [l[0][::-1]] + reve(l[1:]) print(reve(arr)) [[1, 2], [3, 4]] 

What can I write briefly:

 def reve(l): return [l[0][::-1]] + reve(l[1:]) if l else [] 

If you want it in place:

 arr = [[1, 2, 3, 4], [1, 2, 3, 4]] def reve(l): if not l: return # call inplace list.reverse on each sublist l[0].reverse() return reve(l[1:]) reve(arr) 

Output:

 [[4, 3, 2, 1], [4, 3, 2, 1]] 

And finally, we can achieve what you want in place without slicing at all, using iter with the special __length__hint method:

 def reve(l): if l.__length_hint__() == 0: return sub = next(l) sub.reverse() return reve(l) reve(iter(arr)) print(arr) 

Output:

 [[4, 3, 2, 1], [4, 3, 2, 1]] 
+4
source

Both functions can use the map function, but you can also use the imperative for . About my recursive approach, the else statement refers to all cases between the final and second elements of the list, they are concatenated until the first element is reached.

My recursive approach:

 a = [[1, 2, 3], [5, 6, 7]] def mirror(matrix): def revert(row): if len(row) == 1: return [row[0]] else: return [row[-1]] + revert(row[:-1]) # concatenates two lists. return [revert(row) for row in matrix] mirror(a) 

My declarative approach:

 def mirror(matrix): def revert(row): return row[::-1] # copies the array in reverse order return list(map(revert, matrix)) #<-for python3, and just map(...) for python2 mirror(a) 

Both functions come out

[[3, 2, 1], [7, 6, 5]]

+2
source

In fact, a more Pythonic way of doing this would be to use lists. You can do it simply:

 matrix = [[1, 2, 3, 4], [1, 2, 3, 4]] reversed_matrix = (i[::-1] for i in matrix) 

reversed_matrix is โ€‹โ€‹a generator expression. You can convert it to a list by replacing "()" with "[]". In the understanding of the list.

i[::-1] swaps the array in place using the slice operator

+1
source

Source: https://habr.com/ru/post/1237460/


All Articles