我之前已经问过类似的问题,所以我很抱歉,但是我读回任务并误解了最初的需求。排序字典键内的列表 - 从列表中获得最高分3
因此,基于我在这里获得的反馈,这是我使用的代码:
def task3():
classList = {}
classSearch = input("Which class would you like to interrogate? ")
try:
with open("answers " + classSearch + ".txt", 'rb') as handle:
classList = pickle.loads(handle.read())
except IOError as error:
print ("Sorry, this file does not exist")
sortOption = int(input("Would you like sort the students in alphabetical order? Enter 1\n Would you like to sort the students by highest score? Enter 2 \nWould you like to sort students by their average score?Enter 3\n"))
if sortOption == 1:
x = sorted(classList.items())
for key, value in x:
value.sort()
value.reverse()
print (x)
所以我真正需要做的是输出每个学生的最高分,是按字母顺序排序名称。在classList字典里面是学生名字,然后是包含他们在测验中收到的最后3个分数的列表。对于多名学生来说这显然是重复的。任何帮助将大规模赞赏。
标准字典是无序的。这意味着你无法对其进行分类。我想你需要一个有序的词典。 https://docs.python.org/2/library/collections.html#collections.OrderedDict – 2014-12-03 10:56:45
谢谢,我自己最后通过打印索引位置来修复它0 – RH84 2014-12-03 12:27:23