这可能不是最pythonic的方式(我在代码中留下了cmets,希望你能理解,如果不只是问的话):
#Your input list (changed so you can check your requirements when there's two scores for the same person)
input_list = ['John : 6', 'Bob : 10', 'Bob : 9', 'Billy : 2', 'Matthew : 7', 'Jimmy : 2', 'Amy: 9', 'Jenny : 10', 'Michael : 8']
#Transform into a List of tuples
scores = [(item.split(":")[0].strip(), item.split(":")[1].strip()) for item in input_list]
"""CASE 1 - Order alphabetically, and if there is 2 entries for the same person it should only print the highest of the entries for that student"""
print("Entering Case 1")
#create a dictionary with names and delete duplicates (save only the highest score for a given student)
dictionary_by_names = dict()
for item in scores:
if item[0] not in dictionary_by_names:
dictionary_by_names[item[0]] = item[1]
else:
dictionary_by_names[item[0]] = max(int(item[1]), int(dictionary_by_names[item[0]]))
#order by name (List of tuples)
list_by_names = sorted([(key,value) for key, value in dictionary_by_names.items()])
print(list_by_names)
"""CASE 2 - Order by score, taking their highest and ignoring other entries"""
print("Entering Case 2")
#invert dictionary_by_names, since we only want the highest score of a given duplicate
dictionary_by_scores = [(int(value), key) for key, value in dictionary_by_names.items()]
#Sort the list formed from the dictionary (cast the score to int so we can perform sort of int instead of string, because '10' < '2' (strings) and 10 > 2 (int))
list_by_score = sorted([(key,value) for key, value in dictionary_by_scores])
#Cast to string again so we can have it in the "original format"
list_by_score = [(str(item[0]), item[1]) for item in list_by_score]
#Invert tuples order so we can have it in the "original format"
list_by_score = [(item[1], item[0]) for item in list_by_score]
print(list_by_score)
"""CASE 3 - If more than one entry for the same student, calculate the average of their scores, and then order the average of each student high to low"""
print("Entering Case 3")
#create a dictionary with names and averages for students
dictionary_by_avg = dict()
for item in scores:
if item[0] not in dictionary_by_avg:
dictionary_by_avg[item[0]] = float(item[1])
else:
dictionary_by_avg[item[0]] = sum([float(item[1]), float(dictionary_by_avg[item[0]])])/2
list_by_avg = sorted([(value,key) for key, value in dictionary_by_avg.items()])
#Invert tuples order so we can have it in the "original format"
list_by_avg = [(item[1], item[0]) for item in list_by_avg]
print(list_by_avg)
输出:
Entering Case 1
[('Amy', '9'), ('Billy', '2'), ('Bob', 10), ('Jenny', '10'), ('Jimmy', '2'), ('John', '6'), ('Matthew', '7'), ('Michael', '8')]
Entering Case 2
[('Billy', '2'), ('Jimmy', '2'), ('John', '6'), ('Matthew', '7'), ('Michael', '8'), ('Amy', '9'), ('Bob', '10'), ('Jenny', '10')]
Entering Case 3
[('Billy', 2.0), ('Jimmy', 2.0), ('John', 6.0), ('Matthew', 7.0), ('Michael', 8.0), ('Amy', 9.0), ('Bob', 9.5), ('Jenny', 10.0)]
希望这是您想要实现的目标,下次细分您的问题并发布您尝试过的代码块我承认这个问题不是线性的,但您可以完成其中的一部分。
希望这会有所帮助。
顺便说一句,检查一些对您有帮助的文档:
Data Structures
Control Flow Tools
我敢打赌,如果您阅读过它,您本可以构建自己的解决方案。 :)