Sort two lists based on the order of the first

Asked

Viewed 82 times

2

Let me give you an example because it’s clearer to explain:

x = [7,5,9,4,2,3,8,1,10]
y = ['sete', 'cinco', 'nove', 'quatro', 'dois', 'tres', 'oito', 'um', 'seis', 'dez']

I need to sort the list x, so that x[i] = y[i], that is, the lists should look like this:

new_x = [1,2,3,4,5,6,7,8,9,10]
new_y = ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis','sete', 'oito', 'nove', 'dez']

Example:

x[5] = 3
y[5] = 'tres'

After ordering:

new_x[5] = 6
new_y[5] = 'seis'

Basically I would need to order the second list at the same time I order the first one. So that the elements follow paired.

How could I do that?

  • On your list x is missing the number 6, but by the description of the problem, I believe it was typo...

2 answers

2

An alternative is to create a list of indices, and sort these indices according to their value in x.

Then you use these indexes to sort the lists:

x = [7, 5, 9, 4, 2, 3, 8, 1, 6, 10]
y = ['sete', 'cinco', 'nove', 'quatro', 'dois', 'tres', 'oito', 'um', 'seis', 'dez']

# ordene os índices em vez dos elementos em si
indices = list(range(len(x)))
indices.sort(key=lambda i: x[i]) # ordene os índices com relação ao seu respectivo valor em x

# crie as listas baseado na ordem dos índices
new_x = [x[i] for i in indices]
new_y = [y[i] for i in indices]

print(new_x) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(new_y) # ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez']

list(range(len(x))) creates the list with the indexes (in this case, the numbers from zero to the size of x).

Next, we order the indexes, using as a criterion the respective value in the list x. After the sort, the list indices will be [7, 4, 5, 3, 1, 8, 0, 6, 2, 9], which means that x[7] is the first element of x (when this is ordained), x[4] is the second, etc., and the same goes for y.

So just put the lists x and y in the order indicated by the list indices.


Another way to create the already ordered index list would be:

indices = sorted(range(len(x)), key=lambda i: x[i])
  • Thank you very much!! It helped a lot!

  • 2

    @Lucasvasques If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem.

1

Has en route to functional programming using builtin functions zip() to join the two lists, sorted() to order this junction and map() to apply a sorted list separator function:

x = [7, 5, 9, 4, 2, 3, 8, 1, 6, 10]
y = ['sete', 'cinco', 'nove', 'quatro', 'dois', 'tres', 'oito', 'um', 'seis', 'dez']

xy = zip(x, y)                               #Junta as duas listas numa lista d tuplas: [(7, 'sete'), (5, 'cinco'), (9, 'nove'), (4, 'quatro'), (2, 'dois'), (3, 'tres') (8, 'oito'), (1, 'um'), (6, 'seis'), (10, 'dez')] .

sorted_xy = sorted(xy, key= lambda t: t[0])  #Orderna xy com base no primeiro elemento de cada tupla.

new_x= list(map(lambda t: t[0], sorted_xy))  #Extrai o resultado da ordenação da lista x.
new_y= list(map(lambda t: t[1], sorted_xy))  #Extrai o resultado da ordenação da lista y.

print(new_x)
print(new_y)

Resulting:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
['um', 'dois', 'tres', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.