Home > Back-end >  Swapping individual lines of two dimensional arrays
Swapping individual lines of two dimensional arrays

Time:05-14

so I have a question in school I've been trying to solve for a while now, but I just can't figure it out how do do it right. So the task at hand hand is to swap the array lines with the smallest and biggest integer in python, but I just can't figure it out. I know how to find the biggest and smallest integers in the arrays, but have no clue how to swap them. So if I have for example:

array1 = 
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[97, 98, 99, 100]

how do i make it:

array1 =
[97, 98, 99, 100]
[5, 6, 7, 8]
[9, 10, 11, 12]
[1, 2, 3, 4] 

Any input or idea how to solve it on my own is very much appreciated!

CodePudding user response:

Your array is not valid, I assume you meant:

array1 = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [97, 98, 99, 100]]

"A list of lists"

If you're sorting by sum, you can use:

>>> array1.sort(key=sum, reverse=True)
>>> array1
[[97, 98, 99, 100],
 [9, 10, 11, 12],
 [5, 6, 7, 8],
 [1, 2, 3, 4]]

Key functions

CodePudding user response:

int temp = array1[0];
array1[0] = array1[3];
array1[3] = temp;
  • Related