Home > Blockchain >  Python creating 2 lists of name and score for high scores
Python creating 2 lists of name and score for high scores

Time:02-04

I'm trying to append more items to this 2 list after getting the input and score from my game, I need to know how am I able to print both names and score side by side as the code below is what I learnt from geeksforgeeks.org. I'm not really sure if this code is meant for this.

Am I in the right direction as I'm new to this and I need guidance, please advice thanks!

name=["Mary","Lisa","Lumby"]
score=[5,7,4]

def sort_list(list1, list2):
 
    zipped_pairs = zip(list2, list1)
 
    z = [x for _, x in sorted(zipped_pairs)]
 
    return z

print(sort_list(name, score))

The result I currently get is this but I only needed the names without brackets. and I was hoping I can append more results into the list and print the top 3 scores starting with lowest scores. ['Lumby', 'Mary', 'Lisa']

CodePudding user response:

  • zipped_pairs = list(zip(list2, list1)) make zip and convert it into list.
  • sorted_pairs = sorted(zipped_pairs,reverse=True) use to sort the list, reverse=True used to sort from higest to lowest.
  • return sorted_pairs[:3] to return the higest 3 scores
name = ["Mary", "Lisa", "Lumby","Shady","Emad"]
score = [5, 7, 4, 10, 3]

def sort_list(list1, list2):
    zipped_pairs = list(zip(list2, list1))            #output: zipped_pairs =[(5, 'Mary'), (7, 'Lisa'), (4, 'Lumby'), (10, 'Shady'), (3, 'Emad')]
    sorted_pairs = sorted(zipped_pairs,reverse=True)  #output: sorted_pairs = [(10, 'Shady'), (7, 'Lisa'), (5, 'Mary'), (4, 'Lumby'), (3, 'Emad')]
    return sorted_pairs[:3]

result = sort_list(name, score)

for i in result:
    print(f"{i[1]}, {i[0]}")

Output

Shady, 10
Lisa, 7
Mary, 5

CodePudding user response:

You were very close you have all the right code besides the last line here you go (finished code for you)

name=["Mary","Lisa","Lumby"]
score=[5,7,4]

def sort_list(list1, list2):
 
    zipped_pairs = zip(list2, list1)
 
    z = [x for _, x in sorted(zipped_pairs)]
 
    return z
player_scores = sort_list(name,score)
for name in player_scores: 
  print(name)



and if you want something extra

highest_score = []
lowest_score = []

def calculate_Scores(nums):
  i = 1
  max = min = nums[0]
  # first, calculate the max and min.
  for i in nums:  
    if nums[i] > max:
      max = nums[i]    # replace max with the new max
    if nums[i] < min:
      min = nums[i]    # replace min with the new min
    i  = 1
  # now that you've done that, add them to the highest_score and lowest_score
  highest_score.append(max)
  lowest_score.append(min)
 

CodePudding user response:

Rather than parallel lists you should use a dictionary. Something like this:

data = {'Mary': 5, 'Lisa': 7, 'Lumby': 4}

for t in sorted(data.items(), key=lambda e: e[1])[-3:]:
    print(*t)

This will print the top 3 names and scores from the dictionary in ascending order as follows:

Lumby 4
Mary 5
Lisa 7

Now let's do this:

data['Jo'] = 10

Run the code again then:

Mary 5
Lisa 7
Jo 10

If you insist on using a pair of lists then:

name = ["Mary", "Lisa", "Lumby"]
score = [5, 7, 4]

for s, n in sorted(zip(score, name))[-3:]:
    print(n, s)
  • Related