【问题标题】:Find the Second largest number of a nested list programme查找嵌套列表程序的第二大数
【发布时间】:2020-03-14 11:43:28
【问题描述】:

给定 N 个学生的物理课中每个学生的姓名和成绩,将它们存储在嵌套列表中,并打印任何成绩第二低的学生的姓名.

注意:如果有多个年级相同的学生,请按字母顺序排列他们的姓名并将每个姓名打印在一个新行上。

输入

  students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]

以下是我针对上述问题的代码:-

marks = sorted([student[i][1] for i in range(len(student))], reverse = True)

for num in range(1,len(marks)):
    if marks[num] < marks[0]:
        if marks[num+1] < marks[num]:
            temp = marks[num+1]
            break
    else:
        continue
second_top = [student[i] for i in range(len(student)) if student[i][1] == temp]

topper = sorted([k for k,l in second_top])

for i in range(len(topper)):
    print(topper[i])

输出

Berry
Harry

有没有办法改进这段代码。我是 Python 新手,开始练习 Hacker 等级

【问题讨论】:

    标签: python list


    【解决方案1】:

    试试这个,它也占 ex-aequos:

    students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
    
    ranked = sorted(students, key=lambda a: (a[1], a[0]))
    
    second_best = ranked[1][1]  # or whichever rank you want
    
    print(ranked)
    
    ex_aequos = [s for s in ranked if s[1] == second_best]
    
    print(ex_aequos)  
    

    结果:

    [['Akriti', 41], ['Harsh', 39], ['Berry', 37.21], ['Harry',
     37.21], ['Tina', 37.2]]
    [['Berry', 37.21], ['Harry', 37.21]]
    

    【讨论】:

      【解决方案2】:

      首先,我将等级 y 升序排序:

      >>> students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
      >>> grades = sorted(set(g for _, g in students))
      >>> grades
      [37.2, 37.21, 39, 41]
      

      set(g for _, g in students) 行提取成绩并删除重复项 (set)。

      然后取列表的第二个元素,也就是第二低的等级:

      >>> second_lowest_grade = grades[1]
      >>> second_lowest_grade
      37.21
      

      然后对成绩第二低的学生的姓名(按字母顺序)进行循环:

      >>> for name in sorted(n for n, g in students if g == second_lowest_grade):
      ...     print(name)
      ... 
      Berry
      Harry
      

      【讨论】:

        【解决方案3】:

        您可以使用itertools.groupbyitertools.islice 的列表推导:

        from itertools import groupby, islice
        
        s = [i[0]  for e in islice((list(g) for k, g in groupby(sorted(students), key=lambda x: x[1])), 1, 2) for i in e]
        
        print(*s, sep='\n')
        

        输出:

        Berry
        Harry
        

        另一种方法是将您的数据存储在 dict 中,其中键是成绩,值是一个列表,其中包含具有键指定成绩的所有学生:

        from collections import defaultdict
        
        grade_student = defaultdict(list)
        for student, grade in students:
            grade_student[grade].append(student)
        
        # second lowest grade   
        second_grade = sorted(grade_student.keys())[1]
        
        # order the names alphabetically and print each name on a new line
        print(*sorted(grade_student[second_grade]), sep='\n')
        

        输出:

        Berry
        Harry
        

        【讨论】:

          猜你喜欢
          • 2011-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-03
          • 1970-01-01
          • 2023-03-29
          相关资源
          最近更新 更多