I am a beginner to programming and was trying how to learn to flip every alternate row in my 2-D array(python)
For Example:
`Input
a=[[1,2,3,4,5],
[10,9,8,7,6],
[11,12,13,14,15],
[20,19,18,17,16],
[21,22,23,24,25],
[30,29,28,27,26]]'
Output
a_f=[[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25],
[26,27,28,29,30]] `
I tried using flip function from python doc strings but it reversed the whole array. Also is it possible that a code can flip every alternate row without me needing to specify how many rows are there every time.
I suggest you start using NumPy when working with arrays. Here is a small script that performs your task. It uses enumerate to index the rows, modulus (i % 2 == 1) to distinguish between odd and even rows and use the numpy indexing [::-1] to flip the rows:
import numpy as np
def flip_even_rows(a):
for i, row in enumerate(a):
if i % 2 == 1:
a[i] = row[::-1]
return a
a = [[1,2,3,4,5],
[10,9,8,7,6],
[11,12,13,14,15],
[20,19,18,17,16],
[21,22,23,24,25],
[30,29,28,27,26]]
a = np.array(a)
print(flip_even_rows(a))