How to shift the rows in python array?

Question:

I’m trying to find a simple way to shift the position of raw in python array.
And here is my four array

list1 = ['0x68', '0x65', '0x6c', '0x6c']
list2 = ['0x20', '0x68', '0x69', '0x74']
list3 = ['0x6c', '0x65', '0x72', '0x68']
list4 = ['0x65', '0x6c', '0x6c', '0x20']

It will be nice if I can make a function for shift it. For example I might shift it like this

swaps the row elements among each other. It skips the first row. It shifts the elements in the second row, one position to the left. It also shifts the elements from the third row two consecutive positions to the left, and it shifts the last row three positions to the left.

Asked By: james.yi

||

Answers:

they are 4 separate lists so I would iterate trough them by putting them in a list first:

matrix = [list1, list2, list3, list4] 

then I would iterate through them and modify them like this:

new_matrix = [[],[],[],[]]

for i in range(len(matrix)):
    new_matrix[i].extend(matrix[i][i:])
    new_matrix[i].extend(matrix[i][0:i])

for i in range(len(new_matrix)):
    print(new_matrix[i])

I’m sure there is a much better way to instantiate the new_matrix, but this works, the variable contains your 4 arrays shifted

Answered By: l00mad
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.