How to sort student data by grade (ascending) then by name (descending)?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
10
John Seen 3.7
Bill Stark 2.06
Jack Connor 3.47
Drake Mason 3.05
Bill Callum 2.83
Emma Jake 3.65
William Damian 3.33
James Charlie 3.56
Oscar Rhys 3.78
George Reece 2.52
So if there are same "GPA" numbers (value) it should sort it by key (descending).
Following code sorts by value then sorts by key, but ascending
n = int(input())
grades = {}
result = ''
for i in range(n):
student = input().split()
grades[student[0] + ' ' + student[1]] = eval(student[2])
sorted_d = sorted(grades.items(), key=lambda x: (-x[1], x[0]))
for i in sorted_d:
print(str(i[0]) + " - " + str(i[1]))
python sorting
add a comment |
10
John Seen 3.7
Bill Stark 2.06
Jack Connor 3.47
Drake Mason 3.05
Bill Callum 2.83
Emma Jake 3.65
William Damian 3.33
James Charlie 3.56
Oscar Rhys 3.78
George Reece 2.52
So if there are same "GPA" numbers (value) it should sort it by key (descending).
Following code sorts by value then sorts by key, but ascending
n = int(input())
grades = {}
result = ''
for i in range(n):
student = input().split()
grades[student[0] + ' ' + student[1]] = eval(student[2])
sorted_d = sorted(grades.items(), key=lambda x: (-x[1], x[0]))
for i in sorted_d:
print(str(i[0]) + " - " + str(i[1]))
python sorting
1
Please make your data a reproducible example:dat = list[ ('John Seen',3.7), ('Bill Stark',2.06)...]
– smci
Nov 17 '18 at 3:58
add a comment |
10
John Seen 3.7
Bill Stark 2.06
Jack Connor 3.47
Drake Mason 3.05
Bill Callum 2.83
Emma Jake 3.65
William Damian 3.33
James Charlie 3.56
Oscar Rhys 3.78
George Reece 2.52
So if there are same "GPA" numbers (value) it should sort it by key (descending).
Following code sorts by value then sorts by key, but ascending
n = int(input())
grades = {}
result = ''
for i in range(n):
student = input().split()
grades[student[0] + ' ' + student[1]] = eval(student[2])
sorted_d = sorted(grades.items(), key=lambda x: (-x[1], x[0]))
for i in sorted_d:
print(str(i[0]) + " - " + str(i[1]))
python sorting
10
John Seen 3.7
Bill Stark 2.06
Jack Connor 3.47
Drake Mason 3.05
Bill Callum 2.83
Emma Jake 3.65
William Damian 3.33
James Charlie 3.56
Oscar Rhys 3.78
George Reece 2.52
So if there are same "GPA" numbers (value) it should sort it by key (descending).
Following code sorts by value then sorts by key, but ascending
n = int(input())
grades = {}
result = ''
for i in range(n):
student = input().split()
grades[student[0] + ' ' + student[1]] = eval(student[2])
sorted_d = sorted(grades.items(), key=lambda x: (-x[1], x[0]))
for i in sorted_d:
print(str(i[0]) + " - " + str(i[1]))
python sorting
python sorting
edited Nov 17 '18 at 4:03
smci
15.7k679110
15.7k679110
asked Nov 17 '18 at 2:54
Abdulgafur BersugirAbdulgafur Bersugir
489
489
1
Please make your data a reproducible example:dat = list[ ('John Seen',3.7), ('Bill Stark',2.06)...]
– smci
Nov 17 '18 at 3:58
add a comment |
1
Please make your data a reproducible example:dat = list[ ('John Seen',3.7), ('Bill Stark',2.06)...]
– smci
Nov 17 '18 at 3:58
1
1
Please make your data a reproducible example:
dat = list[ ('John Seen',3.7), ('Bill Stark',2.06)...]
– smci
Nov 17 '18 at 3:58
Please make your data a reproducible example:
dat = list[ ('John Seen',3.7), ('Bill Stark',2.06)...]
– smci
Nov 17 '18 at 3:58
add a comment |
1 Answer
1
active
oldest
votes
There are several misunderstandings in your code:
- You chose a dict
grades
with key (student name) and value (GPA/grade) - Bad choice. You can't directly "sort" the tuples of (key, value) of a dictionary. So
dict
is the wrong choice. - Instead you wanted the data to be a list of lists/tuples of
(student,grade)
- So you shouldn't have used a dict for
grades
, you should use a list. (However since it's now a list of(student,grade)
tuples, so call it e.g.sg
orstudent_grades
ordat
or something). - Then your sort function will be pretty simple. If only you didn't need ascending order on col 1, but descending order on col 0, you could do
l.sort(key=operator.itemgetter(1,2))
. But you need different orders, so that forces you to use a customsort(cmp=...)
See e.g. this
I leave it to you to refactor your code. Looks like a fairly simple exercise once you decompose it right. The moral of the story is think carefully about your choice of data structure(s) for the task you need to do: the right choice will make things simple; the wrong choice will create problems, then you need to revisit your initial assumptions and consider the alternatives.
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347780%2fhow-to-sort-student-data-by-grade-ascending-then-by-name-descending%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are several misunderstandings in your code:
- You chose a dict
grades
with key (student name) and value (GPA/grade) - Bad choice. You can't directly "sort" the tuples of (key, value) of a dictionary. So
dict
is the wrong choice. - Instead you wanted the data to be a list of lists/tuples of
(student,grade)
- So you shouldn't have used a dict for
grades
, you should use a list. (However since it's now a list of(student,grade)
tuples, so call it e.g.sg
orstudent_grades
ordat
or something). - Then your sort function will be pretty simple. If only you didn't need ascending order on col 1, but descending order on col 0, you could do
l.sort(key=operator.itemgetter(1,2))
. But you need different orders, so that forces you to use a customsort(cmp=...)
See e.g. this
I leave it to you to refactor your code. Looks like a fairly simple exercise once you decompose it right. The moral of the story is think carefully about your choice of data structure(s) for the task you need to do: the right choice will make things simple; the wrong choice will create problems, then you need to revisit your initial assumptions and consider the alternatives.
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
add a comment |
There are several misunderstandings in your code:
- You chose a dict
grades
with key (student name) and value (GPA/grade) - Bad choice. You can't directly "sort" the tuples of (key, value) of a dictionary. So
dict
is the wrong choice. - Instead you wanted the data to be a list of lists/tuples of
(student,grade)
- So you shouldn't have used a dict for
grades
, you should use a list. (However since it's now a list of(student,grade)
tuples, so call it e.g.sg
orstudent_grades
ordat
or something). - Then your sort function will be pretty simple. If only you didn't need ascending order on col 1, but descending order on col 0, you could do
l.sort(key=operator.itemgetter(1,2))
. But you need different orders, so that forces you to use a customsort(cmp=...)
See e.g. this
I leave it to you to refactor your code. Looks like a fairly simple exercise once you decompose it right. The moral of the story is think carefully about your choice of data structure(s) for the task you need to do: the right choice will make things simple; the wrong choice will create problems, then you need to revisit your initial assumptions and consider the alternatives.
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
add a comment |
There are several misunderstandings in your code:
- You chose a dict
grades
with key (student name) and value (GPA/grade) - Bad choice. You can't directly "sort" the tuples of (key, value) of a dictionary. So
dict
is the wrong choice. - Instead you wanted the data to be a list of lists/tuples of
(student,grade)
- So you shouldn't have used a dict for
grades
, you should use a list. (However since it's now a list of(student,grade)
tuples, so call it e.g.sg
orstudent_grades
ordat
or something). - Then your sort function will be pretty simple. If only you didn't need ascending order on col 1, but descending order on col 0, you could do
l.sort(key=operator.itemgetter(1,2))
. But you need different orders, so that forces you to use a customsort(cmp=...)
See e.g. this
I leave it to you to refactor your code. Looks like a fairly simple exercise once you decompose it right. The moral of the story is think carefully about your choice of data structure(s) for the task you need to do: the right choice will make things simple; the wrong choice will create problems, then you need to revisit your initial assumptions and consider the alternatives.
There are several misunderstandings in your code:
- You chose a dict
grades
with key (student name) and value (GPA/grade) - Bad choice. You can't directly "sort" the tuples of (key, value) of a dictionary. So
dict
is the wrong choice. - Instead you wanted the data to be a list of lists/tuples of
(student,grade)
- So you shouldn't have used a dict for
grades
, you should use a list. (However since it's now a list of(student,grade)
tuples, so call it e.g.sg
orstudent_grades
ordat
or something). - Then your sort function will be pretty simple. If only you didn't need ascending order on col 1, but descending order on col 0, you could do
l.sort(key=operator.itemgetter(1,2))
. But you need different orders, so that forces you to use a customsort(cmp=...)
See e.g. this
I leave it to you to refactor your code. Looks like a fairly simple exercise once you decompose it right. The moral of the story is think carefully about your choice of data structure(s) for the task you need to do: the right choice will make things simple; the wrong choice will create problems, then you need to revisit your initial assumptions and consider the alternatives.
edited Nov 17 '18 at 4:04
answered Nov 17 '18 at 3:56
smcismci
15.7k679110
15.7k679110
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
add a comment |
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
@slider: I don't know for sure that it's a homework exercise, but it sure looks like one. I think giving hints and explaining principles is the best way to respond. (And if the OP does subsequently post code with a specific issue, can help them with that. But not give the full code solution upfront.)
– smci
Nov 17 '18 at 5:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347780%2fhow-to-sort-student-data-by-grade-ascending-then-by-name-descending%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please make your data a reproducible example:
dat = list[ ('John Seen',3.7), ('Bill Stark',2.06)...]
– smci
Nov 17 '18 at 3:58