Advertisement
elena1234

sort tuple in Python

Apr 21st, 2022
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. '''
  2. Question 19:
  3.  
  4.    You are required to write a program to sort the (name, age, score) tuples by ascending order where name is string,
  5.    age and score are numbers. The tuples are input by console. The sort criteria is:
  6.  
  7.    1: Sort based on name
  8.    2: Then sort based on age
  9.    3: Then sort by score
  10.  
  11.    The priority is that name > age > score.
  12.  
  13.    If the following tuples are given as input to the program: '''
  14.  
  15. information = []
  16. while True:
  17.     line = input().split(',')
  18.     if line == ['Stop']:
  19.         break
  20.     name = line[0]
  21.     age = int(line[1])
  22.     score = int(line[2])
  23.     information.append((name, age, score))
  24.  
  25. sorted_information = sorted(
  26.     information, key=lambda tup: (tup[0], tup[1], tup[2]))
  27. string_tuple = [tuple(map(str, tup)) for tup in sorted_information]
  28. print(string_tuple)
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement