字典练习题
1. 给定两个字典,找到它们共有的键存放到一个列表中
dict1 = {'A': 1, 'B': 2, 'C': 3, 'D': 4}
dict2 = {'B': 20, 'D': 40, 'E': 50}
# 1. 给定两个字典,找到它们共有的键存放到一个列表中
dict1 = {'A': 1, 'B': 2, 'C': 3, 'D': 4}
dict2 = {'B': 20, 'D': 40, 'E': 50}
list1 = []
for k, v in dict1.items():
# print(k in dict2)
if k in dict2:
list1.append(k)
print(list1)2. 给定一个字典,找到字典中值最大的键:my_dict = {'A': 10, 'B': 5, 'C': 15, 'D': 20}
# 2. 给定一个字典,找到字典中值最大的键:
my_dict = {'A': 10, 'B': 500, 'C': 150, 'D': 20}
max_v = my_dict['A']
max_k = 'A'
for k, v in my_dict.items():
# print(k, v)
if max_v < v:
max_v = v
max_k = k
print(max_k)3. 字典值的乘积:编写一个程序,计算给定字典中所有值的乘积:my_dict = {'A': 2, 'B': 3, 'C': 4, 'D': 5}
# 3. 字典值的乘积:编写一个程序,计算给定字典中所有值的乘积
my_dict = {'A': 2, 'B': 3, 'C': 4, 'D': 5}
res = 1
for k, v in my_dict.items():
res *= v
print(res)4. 编写一个程序,统计给定列表中每个元素出现的次数,并将结果存储在一个字典中。 my_list = [1, 2, 3, 2, 1, 3, 4, 5, 2, 1]
# 4. 编写一个程序,统计给定列表中每个元素出现的次数,并将结果存储在一个字典中。
# 方法1
# my_list = [1, 2, 3, 2, 1, 3, 4, 5, 2, 1]
# dict1 = {}
# my_list2 = list(set(my_list))
# for i in my_list2:
# dict1.update({i: my_list.count(i)})
# print(dict1)
# 方法2
my_list = [1, 2, 3, 2, 1, 3, 4, 5, 2, 1]
dict_1 = {}
for i in my_list:
if i in dict_1:
dict_1[i] += 1
else:
dict_1[i] = 1
print(dict_1)5.编写一个程序,将两个字典合并,并将相同键对应的值进行加法运算。
dict1 = {'A': 1, 'B': 2, 'C': 3}
dict2 = {'B': 10, 'D': 20, 'C': 30}
# 5.编写一个程序,将两个字典合并,并将相同键对应的值进行加法运算。
dict1 = {'A': 1, 'B': 2, 'C': 3}
dict2 = {'B': 10, 'D': 20, 'C': 30}
dict3 = {}
# 也可以dict3 = dict1.copy()
for k, v in dict1.items():
dict3[k] = v
for k, v in dict2.items():
if k in dict3:
dict3[k] += v
else:
dict3[k] = v
print(dict1)
print(dict2)
print(dict3)6.对列表元素先去重再排序:l = [1, 12, 1, 2, 2, 3, 5]
# 6.对列表元素先去重再排序: l = [1, 12, 1, 2, 2, 3, 5] ll = set(l) l = list(ll) l.sort() print(l)
7. 假设有一个班级的学生成绩数据,数据结构如下
students = [
{"name": "Alice", "score": 85},
{"name": "Bob", "score": 76},
{"name": "Charlie", "score": 90},
{"name": "David", "score": 68},
{"name": "Eva", "score": 92}
]
请编写程序完成以下操作:
计算学生人数。
计算班级总分和平均分。
找出成绩最高和最低的学生。
# 7. 假设有一个班级的学生成绩数据,数据结构如下
students = [
{"name": "Alice", "score": 85},
{"name": "Bob", "score": 76},
{"name": "Charlie", "score": 90},
{"name": "David", "score": 68},
{"name": "Eva", "score": 92}
]
# 请编写程序完成以下操作:
# 计算学生人数。
print(f'学生人数:{len(students)}')
# 计算班级总分和平均分。
res = 0
for i in students:
res += i['score']
print(f'班级总分:{res}')
print(f'平均分:{res / len(students)}')
# 找出成绩最高和最低的学生。
max_score = students[0]['score']
min_score = students[0]['score']
max_student = students[0]['name']
min_student = students[0]['name']
for i in students:
if max_score < i['score']:
max_score = i['score']
max_student = i['name']
if min_score > i['score']:
min_score = i['score']
min_student = i['name']
print(f'最高分:{max_score},最高分学生是:{max_student}')
print(f'最低分:{min_score},最低分学生是:{min_student}')8.假设有一个嵌套字典,表示学生的成绩数据,数据结构如下:
students = { "Alice": {"math": 85, "english": 90, "history": 78}, "Bob": {"math": 76, "english": 82, "history": 88}, "Charlie": {"math": 90, "english": 92, "history": 86}, "David": {"math": 68, "english": 72, "history": 80}, "Eva": {"math": 92, "english": 88, "history": 90} }
打印每个学生的姓名和总分。
打印每个学生的平均分。
# 8.假设有一个嵌套字典,表示学生的成绩数据,数据结构如下:
students = {
"Alice": {"math": 85, "english": 90, "history": 78},
"Bob": {"math": 76, "english": 82, "history": 88},
"Charlie": {"math": 90, "english": 92, "history": 86},
"David": {"math": 68, "english": 72, "history": 80},
"Eva": {"math": 92, "english": 88, "history": 90}
}
# 请编写程序完成以下操作:
# 打印每个学生的姓名和总分。
for name, name_info in students.items():
# print(name)
total = 0
for k, v in name_info.items():
total += v
# print(total)
print(f'姓名:{name},总分{total}')
# 打印每个学生的平均分。
for name, name_info in students.items():
# print(name)
total = 0
for k, v in name_info.items():
total += v
# print(total)
print(f'姓名:{name},平均分{round(total / (len(name_info)), 2)}')9.配置字典中的查询操作: 更新 config 中服务器的端口为 8090,日志级别更新为 DEBUG
config = { "数据库": { "主机": "localhost", "端口": 3306, "用户名": "admin", "密码": "password" }, "服务器": { "IP地址": "192.168.0.1", "端口": 8080, "日志级别": "INFO" }, # ... }
# 9.配置字典中的查询操作: 更新 config 中服务器的端口为 8090,日志级别更新为 DEBUG
config = {
"数据库": {
"主机": "localhost",
"端口": 3306,
"用户名": "admin",
"密码": "password"
},
"服务器": {
"IP地址": "192.168.0.1",
"端口": 8080,
"日志级别": "INFO"
},
# ...
}
# 方法1
# config['服务器']['端口'] = 8090
# config['服务器']['日志级别'] = 'DEBUG'
# print(config)
# 方法2
config['服务器'].update({"端口": 8090,
"日志级别": "DEBUG"})
print(config)