Posts: 353
Threads: 13
Joined: Mar 2020
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
Posts: 94
Threads: 0
Joined: Apr 2020
Try sorted(List of tuples)
Posts: 12,030
Threads: 485
Joined: Sep 2016
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.
Posts: 353
Threads: 13
Joined: Mar 2020
(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)))
Posts: 2,125
Threads: 11
Joined: May 2017
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.
Posts: 353
Threads: 13
Joined: Mar 2020
May-13-2020, 01:50 PM
(This post was last modified: May-13-2020, 01:50 PM by pyzyx3qwerty.)
@ 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)')]
Posts: 1,358
Threads: 2
Joined: May 2019
Drop the quotes you have around each tuple
Posts: 1,950
Threads: 8
Joined: Jun 2018
May-13-2020, 05:23 PM
(This post was last modified: May-13-2020, 05:24 PM by perfringo.)
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.
Posts: 1,358
Threads: 2
Joined: May 2019
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)]
Posts: 353
Threads: 13
Joined: Mar 2020
Thanks for all your replies, I was finally able to solve the code
|