Home > Mobile >  Order one list and have the other list change order so the index of both lists still match
Order one list and have the other list change order so the index of both lists still match

Time:09-18

I have two lists asking for user input that are names and scores, so they relate to each other but aren't connected in any way.

I want to either order by name or score, but if I order either, the name and score doesn't match.

How would I be able to order one list and change the order of the other list so the score for a person is the same as their name?

This is my full code:

test_scores =  []
names = []


while -1 not in test_scores:
    print()
    test_scores.append(int(input("Enter test score:  ")))
    names.append(input("Enter students name:   "))
    
  
test_scores = test_scores[:-1] 
print ()

print(names, test_scores )

order = input("How do you want to order the list? N for by name or S for by score:   ")

if order == "S" or order == "s":
    test_scores = sorted(test_scores, reverse=True) 
elif order == "N" or order == "n":
    names = sorted(names, reverse=False)

print(names, test_scores )

index = 0
for count in range (len(test_scores)):
    print(f"{names[index]} got {test_scores[index]}")
    index =1

CodePudding user response:

The simplest way to do this in Python is to use tuples. Tuples join two other objects together. For example: (99, 'Hanaa')

Also, the zip function can be useful for creating tuples because it 'zips' two lists together:

results = list(zip(test_scores, names))
print(results)

[(99, 'Hanaa'), (85, 'Hans')]

The neat thing about tuples is they are comparable using the usual conditional operators:

print((99, 'Hanaa') > (85, 'Hans'))

True

This means you can sort a list of tuples:

results.sort()
print(results)

[(85, 'Hans'), (99, 'Hanaa')]

Items are sorted according to the first item first, and if that is equal, then it sorts by the second item.

If you want to sort by the second item (name in this example), you either have to rebuild the list of tuples with the item positions swapped or use the key argument in the sort function:

score_sort_key = lambda x: x[0]
name_sort_key = lambda x: x[1]

print(sorted(results, key=name_sort_key))

[(99, 'Hanaa'), (85, 'Hans')]

print(sorted(results, key=score_sort_key))

[(85, 'Hans'), (99, 'Hanaa')]

CodePudding user response:

The problem in your code is that you remove the -1 from the test_scores list but not remove the name of the student who gets the marks -1.

ADD names=names[:-1]

Try this.

test_scores =  []
names = []


while -1 not in test_scores:
    print()
    test_scores.append(int(input("Enter test score:  ")))
    names.append(input("Enter students name:   "))
    
  
test_scores = test_scores[:-1] 
names = names[:-1]
print ()

print(names, test_scores )

order = input("How do you want to order the list? N for by name or S for by score:   ")

if order == "S" or order == "s":
    test_scores = sorted(test_scores, reverse=True) 
elif order == "N" or order == "n":
    names = sorted(names, reverse=False)

print(names, test_scores )

index = 0
for count in range (len(test_scores)):
    print(f"{names[index]} got {test_scores[index]}")
    index =1

OUTPUT

Enter test score:  15
Enter students name:   SHA

Enter test score:  16
Enter students name:   QQQ

Enter test score:  -1
Enter students name:   AAA

['SHA', 'QQQ'] [15, 16]
How do you want to order the list? N for by name or S for by score:   N
['QQQ', 'SHA'] [15, 16]
QQQ got 15
SHA got 16
  • Related