Python 3.4:列表中列表的最小值和最大值不正确

时间:2015-09-26 16:28:29

标签: python max min nested-lists

  1. 我正在创建输出每个学生的最低和最高分数的代码。代码首先从文件中读取,然后将其存储在列表中:
  2. [[' Georgina Cottrell',' 4',' 10',' 8'],[' Amelia Cox& #39;,' 6',' 4',' 8'],[' Jack Elsom',' 8&#39 ;,' 9',' 10'],[' Isabella Jones',' 2',' 8', ' 4']]

    尝试查找最小值和最大值时,它仅适用于某些列表。以下是输出示例:

    [[' Amelia Cox',6,4,8,4,6],[' Georgina Cottrell',4,10,8,4,10],[& #39; Isabella Jones',2,8,4,2,8],[' Jack Elsom',8,9,10,5,9]]

    正如您所看到的,我的代码无法识别某些最小值和一些最大值。

    这是我的代码:

        def menu():
            global user_password
            if user_password == correct_password:
                classes = input("Which class would you like to view? :")
                classes = classes.lower()
                while classes not in ["3a", "4a", "5a"]:
                    time.sleep(2)
                    classes = input("Input invalid. Please enter 3a, 4a or 5a: ")
                    if classes == ["3a", "4a", "5a"]:
                        break
                if classes == "3a":
                    file = open("scores3a.csv", "r")
                    csv_f = csv.reader(file)
                    yourlists = list(csv_f)
                    file.close()
                    yourlists2 = [x for x in yourlists if x]
                    print("-------------------------------------------------------------\n"
                          "Welcome to the menu sytem to view your students scores.\n"
                          "-------------------------------------------------------------\n"
                          "Type the corresponding letter for what you would like to view:\n")
                    time.sleep(2)
                    all_scores = print("Input 'a' to see all of your students scores in alphabetical order(by forename)\n"
                                       "and sorted into highest to lowest scores for each student")
            time.sleep(2)
            highest_scores = print("Input 'b' to see all of the students average scores\n"
                                   "sorted into highest to lowest")
                    user = input()
                    if user == "a":
                        yourlists2.sort()
                        slices = yourlists2[0][:]
                        slices1 = yourlists2[1][:]
                        slices2 = yourlists2[2][:]
                        slices3 = yourlists2[3][:]
                        for row in slices:
                            slices[1]=int(slices[1])
                            slices[2]=int(slices[2])
                            slices[3]=int(slices[3])
                            lowest = min(slices[1:3])
                            slices.append(lowest)
                            highest =  max(slices[1:3])
                            slices.append(highest)
                            break
                        for row in slices1:
                            slices1[1]=int(slices1[1])
                            slices1[2]=int(slices1[2])
                            slices1[3]=int(slices1[3])
                            lowest = min(slices1[1:3])
                            slices1.append(lowest)
                            highest =  max(slices1[1:3])
                            slices1.append(highest)
                            break
                        for row in slices2:
                            slices2[1]=int(slices2[1])
                            slices2[2]=int(slices2[2])
                            slices2[3]=int(slices2[3])
                            lowest = min(slices2[1:3])
                            slices2.append(lowest)
                            highest =  max(slices2[1:3])
                            slices2.append(highest)
                            break
                        for row in slices3:
                            slices3[1]=int(slices3[1])
                            slices3[2]=int(slices3[2])
                            slices3[3]=int(slices3[3])
                            lowest = min(slices3[1:3])
                            slices3.append(lowest)
                            highest =  max(slices3[1:3])
                            slices3.append(highest)
                            break
                        yourlists2=[slices,slices1,slices2,slices3]
                        print(yourlists2)
    
    
        menu()                
    

    如果有人可以提供帮助,我会非常感激。谢谢! :)

0 个答案:

没有答案