Essential notes1.py - 1.2-more_exercises_test 001 \u9009\u53d6 List of List \u7684\u4e00\u884c grades ='Student'Exam 1'Exam 2'Exam

Essential notes1.py - 1.2-more_exercises_test 001...

This preview shows page 1 - 2 out of 2 pages.

### 1.2-more_exercises_test """ 001 选取 List of List 的一行 """ grades = [ ['Student', 'Exam 1', 'Exam 2', 'Exam 3'], ['Thorny', '100', '90', '80'], ['Mac', '88', '99', '111'], ['Farva', '45', '56', '67'], ['Rabbit', '59', '61', '67'], ['Ursula', '73', '79', '83'], ['Foster', '89', '97', '101'] ] students = [i[0] for i in grades[1:]] #['Thorny', 'Mac', 'Farva', 'Rabbit', 'Ursula', 'Foster'] """ 002 创建一个字典 dict, 列值为 key """ #grade_lists['Mac'] == [88, 99, 111] #grade_lists['Ursula'] == [73, 79, 83] grade_lists = {} for i in grades[1:]: grade_lists[i[0]] = [int(g) for g in i[1:]] #{'Foster': [89, 97, 101], 'Rabbit': [59, 61, 67], 'Mac': [88, 99, 111], 'Farva': [45, 56, 67], 'Ursula': [73, 79, 83], #'Thorny': [100, 90, 80]} """ 003 创建一个字典 dict, 列值与行值同时为 key """ #grade_dicts['Foster']['Exam 1'] == 89 #grade_dicts['Rabbit']['Exam 3'] == 67 assignments = grades[0][1:4] #assignments == ['Exam 1', 'Exam 2', 'Exam 3'] grade_dicts = {} for L in grades[1:]: grade_dicts[L[0]] = dict(zip(assignments, [int(g) for g in L[1:]])) #{'Foster': {'Exam 1': 89, 'Exam 2': 97, 'Exam 3': 101}, 'Rabbit': {'Exam 1': 59, 'Exam 2': 61, 'Exam 3': 67}, #'Mac': {'Exam 1': 88, 'Exam 2': 99, 'Exam 3': 111}, 'Farva': {'Exam 1': 45, 'Exam 2': 56, 'Exam 3': 67}, #'Ursula': {'Exam 1': 73, 'Exam 2': 79, 'Exam 3': 83}, 'Thorny': {'Exam 1': 100, 'Exam 2': 90, 'Exam 3': 80}} """ 004 创建一个字典 dict, 通过列值作为 key 返回该列对应的平均值 """ #avg_grades_by_student['Mac'] - 99.333333333
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Fall '19
  • susan lee

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture