0

I have a array in Python given by

matrix = [[10, -9, 8], [1, 15, 5]]

I want to sort the elements of the matrix[0] list so that matrix[1] is organized according to the ascending order of matrix[0]. The expected result would be:

matrix = [[-9, 8, 10], [15, 5, 1]]

My matrix represents values of X and Y. I want to sort X in ascending order so that Y is sorted according to the positions of X, in order to not lose the pairs formed by (X[i], Y[i]).

How can i do that?

  • 2
    Show us what you have tried so far. – Klaus D. Aug 29 '23 at 20:07
  • 1
    Number of rows and columns of matrix will be same or the shape will change? – shaik moeed Aug 29 '23 at 20:07
  • Do you want to modify the existing data structure or create a new one? And does the result need to be a list of lists or can it be a list of tuples? – Kelly Bundy Aug 29 '23 at 20:10
  • My matrix represents values of ```X``` and ```Y```. I want to sort ```X``` in ascending order so that Y is sorted according to the positions of ```X```, in order to not lose the pairs formed by ```(X[i], Y[i])```. – Projetos Programação Aug 29 '23 at 20:15
  • @ProjetosProgramação Are you using or fine to use `numpy`? – shaik moeed Aug 29 '23 at 20:20
  • 1
    How about: `[list(x) for x in zip(*sorted(zip(matrix[0], matrix[1]), key=lambda y:y[1], reverse=True))]` just for fun. Obviously a bit complex so wouldn't typically do this in a single line of code, but just providing for interest's sake. – jarmod Aug 29 '23 at 20:22
  • @jarmod This will sort the both rows, right? – shaik moeed Aug 29 '23 at 20:31
  • @shaikmoeed correct, as the OP requested afaik (see the OP's expected result). I assume the goal is to retain the entire column. – jarmod Aug 29 '23 at 20:47
  • Important terminology note, that is a *list* that contains other *lists* – juanpa.arrivillaga Aug 29 '23 at 20:49
  • 1
    @jarmod In OP, *matrix[1] is organized according to the ascending order of matrix[0]*, I assume, OP wants to sort the first row and arrange values of the second row based on sorted indices of the first row. – shaik moeed Aug 29 '23 at 20:52
  • 1
    @shaikmoeed thanks, I think you're right and I need to swap. Let me check and will fix comments shortly. – jarmod Aug 29 '23 at 20:56
  • 1
    Fix above one-liner: `[list(x) for x in zip(*sorted(zip(matrix[0], matrix[1]), key=lambda y:y[0]))]` – jarmod Aug 29 '23 at 21:01

3 Answers3

1

You can do it easily with numpy, it make your job easy, if you have more matrix related operations, check @Andrej Kesely's answer for plain python version

matrix = np.array([[10, -9, 8], [1, 15, 5]])
matrix[:, matrix[0, :].argsort()]

Output:

array([[-9,  8, 10],
       [15,  5,  1]])
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
0

Try:

matrix = [[10, -9, 8], [1, 15, 5]]

matrix[0] = sorted(enumerate(matrix[0]), key=lambda k: k[::-1])
matrix[0], matrix[1] = [v for _, v in matrix[0]], [matrix[1][i] for i, _ in matrix[0]]

print(matrix)

Prints:

[[-9, 8, 10], [15, 5, 1]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
-1

Here's a working example:

import copy

# Here's your original data
matrix = [[10, -9, 8], [1, 15, 5]]

# Sorting will modify the container, so making a shallow copy (rather than a 
# reference) will keep you for mixing up your output
m_0_orig = copy.copy(matrix[0])

# Now, sort the first list
m_0 = matrix[0]
m_0.sort()

# Using a list comprehension, loop over the sorted elements and get the 
# indexes of the elements prior to the sort, now in the sorted order, from
# matrix[1]
m_1 = [matrix[1][m_0_orig.index(x)] for x in m_0]

# store the result
matrix_result = [m_0,m_1]

# show the result
print(matrix)
print(matrix_result)

## [[-9, 8, 10], [1, 15, 5]]
## [[-9, 8, 10], [15, 5, 1]]
E_net4
  • 27,810
  • 13
  • 101
  • 139
VoNWooDSoN
  • 1,173
  • 9
  • 13