大爽Python入门公开课教案
点击查看教程总目录
本文偏难。
推荐等第一二三四章上完后,回过来拓展阅读。
假设有这样一张成绩表
最左边的一列是名字,起名麻烦。
这里直接用ABC...
来代替。
language | math | english | |
---|---|---|---|
A | 90 | 80 | 85 |
B | 80 | 87 | 86 |
C | 85 | 90 | 90 |
name
值为人名(可以取一个名字做示例值,比如A
),怎么使用name
变量,找到对应的具体成绩信息。(代码不能写死,name
变量值变了也应该能生效)详细信息以以下格式输出
name: A, language: 90, math: 80, english: 85.
language | math | english | |
---|---|---|---|
D | 80 | 85 | 85 |
A: language=90, math=80, english=85, sum=255.
提示,
sum(sequence)
方法可以得到sequence
所有元素的和。
选择列表作为容器的主要实现方式
使用列表作为容器的主要实现方式,其对应的实现方法也有很多种,以下只是一种示例。
scores = [ ("A", 90, 80, 85), ("B", 80, 87, 86), ("C", 85, 90, 90) ]
for
循环遍历查找name
值,代码如下name = "A" for item in scores: item_name = item[0] if item_name == name: print("name: %s, language: %s, math: %s, english: %s." % item)
d_item = ("D", 80, 85, 85) scores.append(d_item)
for item in scores: name, s1, s2, s3 = item s = s1 + s2 + s3 print("%s: language=%s, math=%s, english=%s, sum=%s." % (name, s1, s2, s3, s))
选择字典作为容器的主要实现方式
使用字典作为容器的主要实现方式,其对应的实现方法也有很多种,以下只是一种示例。
scores = { "A": (90, 80, 85), "B": (80, 87, 86), "C": (85, 90, 90) }
name = "A" score_a = scores[name] print("name: %s, language: %s, math: %s, english: %s." % (name, score_a[0], score_a[1], score_a[2]))
scores["D"] = (80, 85, 85)
for name in scores: s1, s2, s3 = scores[name] s = s1 + s2 + s3 print("%s: language=%s, math=%s, english=%s, sum=%s." % (name, s1, s2, s3, s))
考虑以下两个需求,思考原有容器应该如何调整结构(或者说重新设计),
调整后的容器如何实现234。
(其中2和4输出时,只输出学生选择的课程的信息。)
sports
,如何添加这个新的科目的成绩信息到容器中。sports | |
---|---|
A | 85 |
B | 90 |
C | 85 |
D | 85 |
-
代表没有选择这个课程)music | art | science | |
---|---|---|---|
A | - | - | 90 |
B | 90 | - | - |
C | - | 95 | - |
D | - | - | 85 |
思考分析:
# 1 # 用一个专门的列表存储科目信息 courses = ["language", "math", "english"] # 仍然将每行信息看作一组,但使用可变的列表来存储每行的信息 scores = [ ["A", 90, 80, 85], ["B", 80, 87, 86], ["C", 85, 90, 90] ] # 5 courses.append("sports") sports_scores = (85, 90, 85) for i in range(len(scores)): item = scores[i] item.append(sports_scores[i]) # 6 courses.append("music") courses.append("art") courses.append("science") # 没有选择的课程的成绩用-1表示 music_scores = (-1, 90, -1) art_scores = (-1, -1, 95) science_scores = (90, -1, -1) for i in range(len(scores)): item = scores[i] item.append(music_scores[i]) item.append(art_scores[i]) item.append(science_scores[i])
改代码执行完后,
courses
如下
['language', 'math', 'english', 'sports', 'music', 'art', 'science']
scores
如下
[ ['A', 90, 80, 85, 85, -1, -1, 90], ['B', 80, 87, 86, 90, 90, -1, -1], ['C', 85, 90, 90, 85, -1, 95, -1] ]
# 2 # 2 name = "A" for item in scores: item_name = item[0] if item_name == name: # 用output变量来计算最后的输出 output = "name: %s" % item_name for i in range(len(courses)): # 遍历所有课程的索引 si = i + 1 # 成绩的索引为课程索引+1 if item[si] >= 0: # 如果成绩值不小于0,就代表选择了这门课程 course = courses[i] output += ", %s: %s" % (course, item[si]) output += "." print(output) # 3 d_item = ("D", 80, 85, 85, 85, -1, -1, 85) scores.append(d_item) # 4 for item in scores: item_name = item[0] # 用s来计算总分 s = 0 # 用output变量来计算对应的输出 output = "%s: " % item_name for i in range(len(courses)): # 遍历所有课程的索引 si = i + 1 # 成绩的索引为课程索引+1 if item[si] >= 0: # 如果成绩值不小于0,就代表选择了这门课程 course = courses[i] output += "%s=%s, " % (course, item[si]) s += item[si] output += "sum=%s." % s print(output)
其输出为
name: A, language: 90, math: 80, english: 85, sports: 85, science: 90. A: language=90, math=80, english=85, sports=85, science=90, sum=430. B: language=80, math=87, english=86, sports=90, music=90, sum=433. C: language=85, math=90, english=90, sports=85, art=95, sum=445. D: language=80, math=85, english=85, sports=85, science=85, sum=420.
# 1 每个人的成绩信息换成用字典来存储。 # 课程名作为该字典的键,成绩作为该字典的值。 scores = { "A": { "language":90, "math":80, "english":85 }, "B": { "language":80, "math":87, "english":86 }, "C": { "language":85, "math":90, "english":90 } } # 5 sports_scores = { "A": 85, "B": 90, "C": 85 } for name in scores: info = scores[name] info["sports"] = sports_scores[name] # 6 add_scores = { "A": { "science": 90 }, "B": { "music": 90 }, "C": { "art": 95 } } for name in scores: info = scores[name] new_info = add_scores[name] info.update(new_info)
方法介绍:
dict.update(other)
:
Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.
使用其他键/值对更新字典,覆盖现有键。无返回值(直接修改了原有的字典)。
使用示例:>>> a_dict = {"a": 12, "b": 13, "c": 11} >>> other = {"c":15, "d": 20} >>> a_dict.update(other) >>> a_dict {'a': 12, 'b': 13, 'c': 15, 'd': 20}
此时scores
值如下
{ 'A': {'language': 90, 'math': 80, 'english': 85, 'sports': 85, 'science': 90}, 'B': {'language': 80, 'math': 87, 'english': 86, 'sports': 90, 'music': 90}, 'C': {'language': 85, 'math': 90, 'english': 90, 'sports': 85, 'art': 95} }
# 2 name = "A" score_a = scores[name] output = "name: %s" % name for key in score_a: # key即是该同学选择的课程的名称 output += ", %s: %s" % (key, score_a[key]) output += "." print(output) # 3 score_d = { "language": 80, "math": 85, "english": 85, "sports": 85, "science": 85 } scores["D"] = score_d # 4 for name in scores: info = scores[name] output = "%s: " % name s = 0 for key in info: # key即是该同学选择的课程的名称 output += "%s=%s, " % (key, info[key]) s += info[key] output += "sum=%s." % s print(output)
其输出为
name: A, language: 90, math: 80, english: 85, sports: 85, science: 90. A: language=90, math=80, english=85, sports=85, science=90, sum=430. B: language=80, math=87, english=86, sports=90, music=90, sum=433. C: language=85, math=90, english=90, sports=85, art=95, sum=445. D: language=80, math=85, english=85, sports=85, science=85, sum=420.