如何在python中创建一个函数

时间:2015-02-25 23:18:36

标签: python dictionary

我试图从这大块代码中创建3个独立的函数。一个用于计算字典中按键的字母顺序,一个用于计算字典中值的最高分数,另一个用于从字典中查找平均分数

这是三个词典:

class_1={"Mike":[2,3,2,5,3],"Jack":[1,2,1,6,4],"Rob":[0,2,6,4]}
class_2={"Steve":[2,5,2,5,9],"Mo":[3,2,1,6,4],"Bill":[9,2,6,4]}
class_3={"Harry":[1,3,1,5,3],"Dave":[7,2,1,6,4],"Molly":[3,2,9,4]}

使用这一大块代码,我可以做我想要实现的目标,但它看起来并不优雅或高效。

class_choice_menu=input("Please choose the class you would like to check:\n\
1.Class 1\n\
2.Class 2\n\
3.Class 3\n")
class_choice=int(class_choice_menu)
if class_choice ==1:
    class_1_average = {key: int(sum(value)/len(value)) for key, value in class_1.items()}
    max_each_1 = {key: max(value) for key, value in class_1.items()}
    print("Alphabetically")
    for key in sorted(max_each_1.keys()):
        print ("%s: %s" % (key, max_each_1[key]))
    print ('\nSort by average scores(descending order):')
    for key, value in sorted(class_1_average.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, class_1_average[key]))
    print ('\nSort by high scores(descending order):')
    for key, value in sorted(max_each_1.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, max_each_1[key]))        
elif class_choice ==2:
    class_2_average = {key: int(sum(value)/len(value)) for key, value in class_2.items()}
    max_each_2 = {key: max(value) for key, value in class_2.items()}
    print("Alphabetically")
    for key in sorted(max_each_2.keys()):
        print ("%s: %s" % (key, max_each_2[key]))
    print ('\nSort by average scores(descending order):')
    for key, value in sorted(class_2_average.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, class_2_average[key]))
    print ('\nSort by high scores(descending order):')
    for key, value in sorted(max_each_2.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, max_each_2[key])) 
elif class_choice ==3:
    class_3_average = {key: int(sum(value)/len(value)) for key, value in class_3.items()}
    max_each_3 = {key: max(value) for key, value in class_3.items()}
    print("Alphabetically")
    for key in sorted(max_each_3.keys()):
        print ("%s: %s" % (key, max_each_3[key]))
    print ('\nSort by average scores(descending order):')
    for key, value in sorted(class_3_average.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, class_3_average[key]))
    print ('\nSort by high scores(descending order):')
    for key, value in sorted(max_each_3.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, max_each_3[key]))

我曾经去过一个,但它似乎没有工作

def alphabetical(class_choice):
    max_each_%s= {key:max(value) for key,value in class_%s.items()} 
    print("Alphabetically")
    for key in sorted (max_each_%s.keys()):
        print("%s: %s" % (key,max_each_%s[key] % class_choice))

1 个答案:

答案 0 :(得分:0)

我知道你需要什么。

首先,您需要了解如何定义函数。

  

函数是一个有组织的,可重用的代码块,用于执行单个相关操作。函数为您的应用程序提供了更好的模块化,并且可以重复使用高级代码。

让我们查看您的代码。

我建议你想扩展类变量,它易于维护。它可以基于用户输入和if-else语句来返回您选择的内容。

def get_class(num):
    if num == 1:
        return class_1
    elif num == 2:
        return class_2
    elif num == 3:
        return class_3

为了简单的代码。我们可以重复使用此代码。

def calculate(selected_class):
    class_average = {key: int(sum(value)/len(value)) for key, value in selected_class.items()}
    max_each_student = {key: max(value) for key, value in selected_class.items()}
    return (class_average, max_each_student)

def Alphabetically(max_each_student):
    print("Alphabetically")
    for key in sorted(max_each_student.keys()):
        print ("%s: %s" % (key, max_each_student[key]))

def sort_by_average_scores(average):
    print ('\nSort by average scores(descending order):')
    for key, value in sorted(average.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, average[key]))

def sort_by_high_scores(max_each_student):
    print ('\nSort by high scores(descending order):')
    for key, value in sorted(max_each_student.items(), reverse = True, key=lambda item: (item[1], item[0])):
        print ("%s: %s" % (key, max_each_student[key]))

如果要处理数据,只需使用这些功能即可。它也可以这样做。

the_class = get_class(class_choice)
the_class_average, max_each_student_of_the_class= calculate(the_class)
Alphabetically(max_each_student_of_the_class)
sort_by_average_scores(the_class_average)
sort_by_high_scores(max_each_student_of_the_class)