Files
2025PY/day18/04-字典的应用.py
2025-05-22 16:50:44 +08:00

32 lines
862 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1.创建一个字典包含5个学生的姓名和他们的成绩。找出成绩最高的学生及其成绩
# 创建包括5个学生姓名和成绩的字典
students = {
'张三':78,
'李四':80,
'王五':66,
'赵六':77,
'孙七':58
}
# max函数输出括号里面的最大数字
# min函数输出括号里面的最小数字
print(max(234,45,3456,23)) # 3456
print(min(234,45,3456,23)) # 23
# 2.字典的遍历
max = 0
for item in students:
if students[item] > max:
max = students[item]
print(max)
# 练习:创建一个字典包含一些国家及其人口。根据人口从大到小排序并输出前3个国家
dict_sort = {
'中国':14,
'美国':3,
'印度':13,
'日本':1,
'墨西哥':2
}
print(list(dict_sort.items()))
list = list(dict_sort.items())
print(sorted(list,key=lambda x:x[1]))