0

I have two list which their values match with each others positions. So Jon's score would be 123 and Bede's would be 11 etc.

name = ["Jon", "Bede", "Joe"]
score = [123, 11, 43]

How is it possible to order the the list so that it outputs the smallest score first ascending to the highest score, each time outputting the name of the person who scored it.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Zak
  • 43
  • 4
  • why do you want to do this? whats the reason/requirement? – John Ruddell Nov 01 '15 at 10:04
  • 1
    @Zak: Do you need *new* lists or to *modify* the initial lists? – P. Ortiz Nov 01 '15 at 10:19
  • Does this answer your question? [How to sort two lists (which reference each other) in the exact same way](https://stackoverflow.com/questions/9764298/how-to-sort-two-lists-which-reference-each-other-in-the-exact-same-way) – Stef Jan 03 '22 at 14:59

6 Answers6

5
for s, n in sorted(zip(score, name)): # sort by score.
    print(n, s)
dopstar
  • 1,478
  • 10
  • 20
1

If you have two lists, where the indices are correlated, it is very difficult to keep them in sync. The solution python has, is one list of tuples, where the first element in each tuple is the name and the second one is the score:

names_with_score = [
    ("Jon", 123),
    ("Bede", 11),
    ("Joe", 43),
]
names_with_score.sort(key=lambda x: x[1]) # sort with the second element

If you have no control over how the data is delivered, you might join to lists with the zip-function:

names_with_score = zip(name, score)
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

What about this one?

name = ["Jon", "Bede", "Joe"]
score = [123, 11, 43]

for i in sorted(zip(name, score), key=lambda x:x[1]):
    print(i[0], i[1], sep=': ')

Output:

Bede: 11
Joe: 43
Jon: 123
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • The list comprehension solution is logically not right. Printing inside list comp is not pythonic. In fact its illegal in python2. This is more like abusing a list comp. – dopstar Nov 01 '15 at 10:17
  • @dopstar Thanks, don't know a lot about Python 2. I'll remove them :) – Remi Guan Nov 01 '15 at 10:18
  • 1
    @Kevin Your code seems to be incorrect, you are doing sorting by name first. Try with this input : `name = ["Dean", "Bede", "Joe", "Al"];score = [123, 11, 43, 43]` – P. Ortiz Nov 01 '15 at 10:24
  • 1
    @Kevin: seems OK now ;) – P. Ortiz Nov 01 '15 at 10:29
0

This merges the lists, if you don't mind that:

sorted(zip(name,score))
Jameson
  • 6,400
  • 6
  • 32
  • 53
  • 2
    This does not *merge* the two lists. It just zips them up and sort the zipped list of tuples by the first element which is the name. – dopstar Nov 01 '15 at 10:28
  • lol okay my bad according to zip() documentation it "aggregates" elements. This is why python is the way it is. By the way. – Jameson Nov 01 '15 at 10:33
  • 1
    English is not my first language, bear with me. But I think aggregating things is to 'bundle' them up like `zip` is doing while on the other hand `merging` lists would be more like *exteding* one list with the other one with some rules whether to keep or discard duplicates (more like set union, but for lists). I may be wrong. – dopstar Nov 01 '15 at 10:37
0

Instead of sticking with a pair of parallel lists, try turning them into a dictionary for easier access. You can then sort the dictionary by its keys with the key argument:

name = ["Jon", "Bede", "Joe"]
score = [123, 11, 43]
results = dict(zip(name, score))
for k in sorted(results, key=results.get):
    print('{}: {}'.format(k, results[k]))

Result:

Bede: 11
Joe: 43
Jon: 123
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

You can use zip(...) to aggregate the two lists together; then sort; then use zip(*...) to unzip the result.

name = ["Jon", "Bede", "Joe"]
score = [123, 11, 43]
newscore, newname = map(list, zip(*sorted(zip(score, name))))
print(newname)
print(newscore)
# ['Bede', 'Joe', 'Jon']
# [11, 43, 123]
Stef
  • 13,242
  • 2
  • 17
  • 28