Python Forum
Sort on basis of (name,age,score)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort on basis of (name,age,score)
#1
You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:
1: Sort based on name;
2: Then sort based on age;
3: Then sort by score.
The priority is that name > age > score.
If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]

I'm stuck on this and have no idea how to start. Any help would be appreciated
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#2
Try
sorted(List of tuples)
Reply
#3
Quote:I'm stuck on this and have no idea how to start. Any help would be appreciated
Make a serious effort, then post your code.
We are glad to help, but will not write it for you.
Reply
#4
(May-12-2020, 06:23 PM)Larz60+ Wrote: Make a serious effort, then post your code.
Here you go. Also, i forgot to mention the hint -
Quote:In case of input data being supplied to the question, it should be assumed to be a console input.
We use itemgetter to enable multiple sort keys.
from operator import itemgetter
list1 = []
while True:
    input_list = input()
    if not input_list:
        break
    list1.append(tuple(input_list.split(",")))

print(sorted(list1, key=itemgetter(0,1,2)))
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
Quote:
print(sorted(list1, key=itemgetter(0,1,2)))

Could be written as:

print(sorted(list1))
sort does also compare the elements in sequences.
It's allowed to have different data-types per field, but they must be the same over the whole list.
L = [(1, "a"), (1, "A")]

print(sorted(L))
Output:
[(1, 'A'), (1, 'a')]
Without the use of key, sort compares the whole element (1, 'A') with (1, "a").
The ASCII character A has the value 65 and a is 97.
The default order is from small > big. Sort compares the first value, then the second and so far.
If you don't have to exclude values from the tuple, the use of key is not necessary.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
@DeaD_EyE, even if I use
print(sorted(list1))
it gives the same output
Output:
[('(Tom', '19', '80)', '(John', '20', '80)', '(Jony', '17', '91)', '(Jony', '17', '93)', '(Json', '21', '85)')]
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#7
Drop the quotes you have around each tuple
Reply
#8
As DeaD_EyE already pointed out - watch out string comparisons. Current dataset is carefully selected.

If age is provided as string then:

>>> '9' < '29'
False
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
Your problem is where you have the quotes in the string -
inlist = [('Tom',19,80),('John',20,90),('Jony',17,91),('Jony',17,93),('Json',21,85)]
print(sorted(inlist))
Output:
[('John', 20, 90), ('Jony', 17, 91), ('Jony', 17, 93), ('Json', 21, 85), ('Tom', 19, 80)]
Reply
#10
Thanks for all your replies, I was finally able to solve the code
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 2,478 Nov-08-2022, 12:37 AM
Last Post: Mavoz
  how to sort a list without .sort() function letmecode 3 3,445 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,173 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 49,001 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  Average score MartinEvtimov 5 6,854 Apr-02-2017, 07:35 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020