1、练习1(字典)
字典dic,dic = {'k1':'v1', 'k2': 'v2', 'k3': [11,22,33]}
(1). 请循环输出所有的key
python
dic = {"k1": "v1", "k2": "v2", "k3": [11, 22, 33]}
for k in dic.keys():
print(k)
k1
k2
k3
(2). 请循环输出所有的value
python
for v in dic.values():
print(v)
v1
v2
[11, 22, 33]
(3). 请循环输出所有的key和value
python
for k, v in dic.items():
print(k, v)
k1 v1
k2 v2
k3 [11, 22, 33]
(4). 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典
python
dic["k4"] = "v4"
print(dic)
{'k1': 'v1', 'k2': 'v2', 'k3': [11, 22, 33], 'k4': 'v4'}
(5). 请在修改字典中 "k1" 对应的值为 "kidd",输出修改后的字典
python
dic["k1"] = "kidd"
print(dic)
{'k1': 'kidd', 'k2': 'v2', 'k3': [11, 22, 33], 'k4': 'v4'}
(6). 请在k3对应的值中追加一个元素 44,输出修改后的字典
python
dic["k3"].append(44)
print(dic)
{'k1': 'kidd', 'k2': 'v2', 'k3': [11, 22, 33, 44], 'k4': 'v4'}
(7). 请在k3对应的值的第 1 个位置插入个元素 18,输出修改后的字典
python
dic["k3"].insert(1, 18)
print(dic)
{'k1': 'kidd', 'k2': 'v2', 'k3': [11, 18, 22, 33, 44], 'k4': 'v4'}
2、练习2(列表字典)
有如下值li= [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值列表, 'k2': 小于66的所有值列表}
python
li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
v1 = [i for i in li if i > 66]
v2 = [j for j in li if j < 66]
res_dic = {"k1":v1,"k2":v2}
print(res_dic)
{'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55]}
3、练习3(字典)
- 创建一个字典,包含你最喜欢的三种水果和它们的价格。
- 打印字典中所有键值对。
python
dic_fruits = {"apple": 2, "banana": 4, "orange": 5}
for k, v in dic_fruits.items():
print(k, v)
apple 2
banana 4
orange 5
- 添加一种新的水果和它的价格。
python
dic_fruits["mango"] = 7
print(dic_fruits)
{'apple': 2, 'banana': 4, 'orange': 5, 'mango': 7}
- 修改一种水果的价格。
python
dic_fruits["apple"] = 3
print(dic_fruits)
{'apple': 3, 'banana': 4, 'orange': 5, 'mango': 7}
- 删除一种水果及其价格。
python
del dic_fruits['banana']
print(dic_fruits)
{'apple': 3, 'orange': 5, 'mango': 7}
4、练习4(字典)
- 创建一个字典,包含你的朋友的姓名和他们的年龄。
- 使用循环打印出每个朋友的姓名和年龄。
python
frinds_dic = {"zl": 23, "zff": 24, "jwj": 22}
for k, v in frinds_dic.items():
print(f"姓名:{k}, 年龄:{v}")
姓名:zl, 年龄:23
姓名:zff, 年龄:24
姓名:jwj, 年龄:22
- 将朋友的年龄加上 1,并更新字典中对应的值。
python
frinds_dic = {"zl": 23, "zff": 24, "jwj": 22}
for name,age in frinds_dic.items():
age = age + 1
frinds_dic[name] = age
print(frinds_dic)
{'zl': 24, 'zff': 25, 'jwj': 23}
5、练习5(列表字典)
python
# 声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、手机号、性别(男、女、不明)
student = {"name": "张三", "age": "23", "score": 88, "tel": "23423532", "gender": "男"}
# 声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [
{"name": "张三", "age": 23, "score": 88, "tel": "23423532", "gender": "男"},
{"name": "李四", "age": 26, "score": 80, "tel": "12533453", "gender": "女"},
{"name": "王五", "age": 15, "score": 58, "tel": "56453453", "gender": "男"},
{"name": "赵六", "age": 16, "score": 57, "tel": "86786785", "gender": "不明"},
{"name": "小明", "age": 18, "score": 98, "tel": "23434656", "gender": "女"},
{"name": "小红", "age": 23, "score": 72, "tel": "67867868", "gender": "女"},
]
1.统计不及格学生的个数
2.打印不及格学生的名字和对应的成绩
python
students = [
{"name": "张三", "age": 23, "score": 88, "tel": "23423532", "gender": "男"},
{"name": "李四", "age": 26, "score": 80, "tel": "12533453", "gender": "女"},
{"name": "王五", "age": 15, "score": 58, "tel": "56453453", "gender": "男"},
{"name": "赵六", "age": 16, "score": 57, "tel": "86786785", "gender": "不明"},
{"name": "小明", "age": 18, "score": 98, "tel": "23434656", "gender": "女"},
{"name": "小红", "age": 23, "score": 72, "tel": "67867868", "gender": "女"},
]
num = 0
for i in students:
if i["score"] < 60:
num += 1
print(i["name"],i["score"])
print(num)
王五 58
赵六 57
2
3.统计未成年学生的个数
python
teen_student = 0
for i in students:
if i["age"] < 18:
teen_student += 1
print(teen_student)
2
4.打印手机尾号是8的学生的名字
python
for i in students:
if i["tel"][-1] == "8":
print(i["name"])
小红
5.打印最高分和对应的学生的名字
python
max_score = 0
for i in students:
if i["score"] > max_score:
max_score = i["score"]
max_student = i["name"]
print(max_score,max_student)
98 小明
6.将列表按学生成绩从大到小排序
python
scores =[i["score"] for i in students]
print(sorted(scores,reverse=True))
[98, 88, 80, 72, 58, 57]
7.删除性别不明的所有学生
python
for i in students:
if i["gender"]=="不明":
students.remove(i)
print(students)
[{'name': '张三', 'age': 23, 'score': 88, 'tel': '23423532', 'gender': '男'}, {'name': '李四', 'age': 26, 'score': 80, 'tel': '12533453', 'gender': '女'}, {'name': '王五', 'age': 15, 'score': 58, 'tel': '56453453', 'gender': '男'}, {'name': '小明', 'age': 18, 'score': 98, 'tel': '23434656', 'gender': '女'}, {'name': '小红', 'age': 23, 'score': 72, 'tel': '67867868', 'gender': '女'}]
6、练习6(列表)
python
#用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese = ['小明','小张','小黄','小杨']
math = ['小黄','小李','小王','小杨','小周']
english = ['小杨','小张','小吴','小冯','小周']
python
# 1、求选课学生总共有多少人
chinese = ["小明", "小张", "小黄", "小杨"]
math = ["小黄", "小李", "小王", "小杨", "小周"]
english = ["小杨", "小张", "小吴", "小冯", "小周"]
students = []
for i in chinese + math + english:
if i not in students:
students.append(i)
print(students)
print(len(students))
['小明', '小张', '小黄', '小杨', '小李', '小王', '小周', '小吴', '小冯']
9
python
#2、求只选了第一个学科的人的数量和对应的名字
num = 0
for i in chinese:
if i not in math + english:
print(i)
num += 1
print(num)
小明
1
python
# 3、求只选了一门学科的学生的数量和对应的名字
set1 = set(chinese)
set2 = set(math)
set3 = set(english)
num1 = 0
for i in students:
if i not in (set1 & set2) | (set1 & set3) | (set2 & set3):
num1 += 1
print(i)
print(num1)
小明
小李
小王
小吴
小冯
5
python
# 4、求只选了两门学科的学生的数量和对应的名字(选做)
num4 = 0
for i in students:
if i in ((set1 & set2) | (set1 & set3) | (set2 & set3)) - (set1 & set2 & set3):
num4 += 1
print(i)
print(num4)
小张
小黄
小周
3
python
#5、求选了三门学生的学生的数量和对应的名字(选做)
num5 = 0
for i in students:
if i in set1 & set2 & set3:
num5 += 1
print(i)
print(num5)
小杨
1