0

Let's suppose we have the following list containing some document names:

documents = ['document1.txt', 'document2.txt', 'document3.txt', 'document4.txt', 'document5.txt']

And we also have another python list containing some metric related to the documents above:

metrics = [0.2, 0.55, 0.4, 0.8, 0.4]

where metrics[0] corresponds to documents[0], metrics[1] to documents[1] and so on.

Is there any good way to order the list documents on ascending order based on their corresponding metrics values?

Output should be:

ordered_documents = ['document1.txt', 'document3.txt', 'document5.txt', 'document2.txt', 'document4.txt']

or

ordered_documents = ['document1.txt', 'document5.txt', 'document3.txt', 'document2.txt', 'document4.txt']
Moonstone5629
  • 58
  • 1
  • 6
  • You can use `sorted`, with a custom key they tell python how to sort: `documents = sorted(documents, key=lambda x:metrics[documents.index(x)])` – Red Dec 09 '20 at 13:49

2 Answers2

1

You could zip the two lists and sort, then retain just the name in a list comprehension:

[v for k, v in sorted(zip(metrics, documents))]

# out:
['document1.txt',
 'document3.txt',
 'document5.txt',
 'document2.txt',
 'document4.txt']
Pierre D
  • 24,012
  • 7
  • 60
  • 96
0

This can be done using Pandas:

import pandas as pd

documents = ['document1.txt', 'document2.txt', 'document3.txt', 'document4.txt', 'document5.txt']
metrics = [0.2, 0.55, 0.4, 0.8, 0.4]

df = pd.DataFrame({'metrics':metrics, 'documents':documents}).sort_values(by='metrics', ascending=True)

And then take the column you want.

new_metrics = df['metrics']
new_documents = df['documents']
Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33