字典的案例演示

import random

# 初始化学生的基本信息

dic_student_info = {95001: "王一", 95002: "胡二", 95003: "张三", 95004: "李四", 95005: "赵五",

95006: "马六", 95007: "杨七", 95008: "刘八", 95008: "孙九", 95010: "陈十"}

# 生成学生成绩

student_result = []

for i in range(len(dic_student_info)):

temp_result = []

for j in range(3):

temp_result.append(random.randint(50,100))

# 添加到student_result

student_result.append(temp_result)

# 打印生成的成绩

print(student_result)

# 生成总分的list

total_result = []

for i in student_result:

total_result.append(sum(i))

# 把总分倒叙排列

total_result.sort(reverse=True)

print(total_result)

# 组合存储结构

total_student_result = {}

# 获得学生信息的key

student_sno = list(dic_student_info)

# 遍历

index = 0

for i in student_sno:

temp_total = {}

temp_total["姓名"] = dic_student_info[i]

temp_total["明细"] = list(student_result)[index]

temp_total["总分"] = sum(list(student_result)[index])

temp_total["名次"] = total_result.index(sum((student_result)[index])) + 1

total_student_result[i] = temp_total

index += 1

for i in total_student_result:

print(i, ":", total_student_result[i])

# 打印一张成绩倒叙排名表【名次，学号，姓名，语文，数学，英语，总分，均分】

print("\n \n ###########################成绩统计表##############################")

print("名次     学号       姓名       语文    数学    英语     总分    均分")

print("=====================================================================")

for i in range(len(dic_student_info)):

for j in total_student_result:

if total_student_result[j]["名次"] == i + 1:

print(i+1, end="\t \t")

print(j, end="\t \t")

print(total_student_result[j]["姓名"], end="\t \t")

print(total_student_result[j]["明细"][0], end="\t \t")

print(total_student_result[j]["明细"][1], end="\t \t")

print(total_student_result[j]["明细"][2], end="\t \t")

print(total_student_result[j]["总分"], end="\t \t")

print("%.2f"%(total_student_result[j]["总分"]/3))

break

else:continue

print("=====================================================================")

C:\python\python.exeC:/python/demo/file3.py

[[77, 57, 61], [64, 77, 65], [88, 88,83], [95, 75, 100], [57, 79, 88], [59, 90, 90], [88, 68, 80], [59, 79, 89],[58, 55, 73]]

[270, 259, 239, 236, 227, 224, 206,195, 186]

95001 : {'姓名':'王一','明细':[77, 57, 61], '总分': 195, '名次': 8}

95002 : {'姓名':'胡二','明细':[64, 77, 65], '总分': 206, '名次': 7}

95003 : {'姓名':'张三','明细':[88, 88, 83], '总分': 259, '名次': 2}

95004 : {'姓名':'李四','明细':[95, 75, 100], '总分': 270, '名次': 1}

95005 : {'姓名':'赵五','明细':[57, 79, 88], '总分': 224, '名次': 6}

95006 : {'姓名':'马六','明细':[59, 90, 90], '总分': 239, '名次': 3}

95007 : {'姓名':'杨七','明细':[88, 68, 80], '总分': 236, '名次': 4}

95008 : {'姓名':'孙九','明细':[59, 79, 89], '总分': 227, '名次': 5}

95010 : {'姓名':'陈十','明细':[58, 55, 73], '总分': 186, '名次': 9}

###########################成绩统计表##############################

=====================================================================

1                95004                李四                95                75                100                270                90.00

2                95003                张三                88                88                83                259                86.33

3                95006                马六                59                90                90                239                79.67

4                95007                杨七                88                68                80                236                78.67

5                95008                孙九                59                79                89                227                75.67

6                95005                赵五                57                79                88                224                74.67

7                95002                胡二                64                77                65                206                68.67

8                95001                王一                77                57                61                195                65.00

9                95010                陈十                58                55                73                186                62.00

Process finished with exit code 0

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120