a.遍历字典 dic 中所有的key
b.遍历字典 dic 中所有的value
c.循环遍历字典 dic 中所有的key和value
d.添加一个键值对"k4","v4",输出添加后的字典 dic
e.删除字典 dic 中的键值对"k1","v1",并输出删除后的字典 dic
f.删除字典 dic 中 'k5' 对应的值,若不存在,使其不报错,并返回None
g.获取字典 dic 中“k2”对应的值
h.已知字典dic2 = {'k1':"v111",'a':"b"} 编写程序,使得dic2 = {'v111':"k1",'b':"a"}
dic = {"k1": "v1", "k2": "v2", "k3": "v3"}
#a
for key in dic:
print(key)
#b
for value in dic.values():
print(value)
#c
for key,value in dic.items():
print(key,value)
#d
dic["k4"] = "v4"
print(dic)
#e
dic.pop("k1")
print(dic)
#f
if dic.get("k5"):
result = dic.pop("k5")
print(result)
else:
print(None)
#g
value = dic["k2"]
#h
dic2 = {'k1':"v111",'a':"b"}
dic3 = {}
for key,value in dic2.items():
dic3[value] = key
dic2 = dic3
print(dic2)
list1 = [11,22,11,33,44,55,66,55,66]
dict1 = {}
for num in list1:
if num not in dict1:
dict1[num] = 1
else:
dict1[num] += 1
students = [
{'name': '小花', 'age': 19, 'score': 90, 'gender': '女', 'tel':
'15300022839'},
{'name': '明明', 'age': 20, 'score': 40, 'gender': '男', 'tel':
'15300022838'},
{'name': '华仔', 'age': 18, 'score': 90, 'gender': '女', 'tel':
'15300022839'},
{'name': '静静', 'age': 16, 'score': 90, 'gender': '不明', 'tel':
'15300022428'},
{'name': 'Tom', 'age': 17, 'score': 59, 'gender': '不明', 'tel':
'15300022839'},
{'name': 'Bob', 'age': 18, 'score': 90, 'gender': '男', 'tel':
'15300022839'}
]
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.删除性别不明的所有学生
g.将列表按学生成绩从大到小排序
# a.统计不及格学生的个数
count = 0
for stu_dict in students:
if stu_dict["score"] < 60:
count += 1
print("不及格学生的个数:%d" % (count))
# b.打印不及格学生的名字和对应的成绩
for stu_dict in students:
if stu_dict["score"] < 60:
print("姓名:%s,成绩:%d" % (stu_dict['name'],stu_dict['score']))
# c.统计未成年学生的个数
count = 0
for stu_dict in students:
if stu_dict["age"] < 18:
count += 1
print("未成年学生的个数:%d" % (count))
# d.打印手机尾号是8的学生的名字
for stu_dict in students:
if stu_dict["tel"][-1] == "8":
print("姓名:%s" % (stu_dict['name']))
# e.打印最高分和对应的学生的名字
# 方式一
max_score = students[0]['score']
for stu_dict in students:
if stu_dict['score'] > max_score:
max_score = stu_dict['score']
# 方式二
max_score = max([stu_dict["score"] for stu_dict in students])
print("最高成绩为:%d" % (max_score))
for stu_dict in students:
if stu_dict['score'] == max_score:
print("姓名:%s,成绩:%d" % (stu_dict['name'],max_score))
# f.删除性别不明的所有学生
# 方式一
new_students = []
for stu_dict in students:
if stu_dict["gender"] != "不明":
new_students.append(stu_dict)
print(new_students)
# 方式二
for stu_dict in students[:]:
if stu_dict['gender'] == "不明":
students.remove(stu_dict)
print(students)
# g.将列表按学生成绩从大到小排序
# 方式一:冒泡排序
for i in range(len(students) - 1):
for j in range(len(students) - i - 1):
# 比较的是成绩,交换的是字典
if students[j]["score"] < students[j + 1]['score']:
students[j],students[j + 1] = students[j + 1],students[j]
print(students)
# 扩展:方式二:sort(key),key用于指定排序的规则,此处使用的是匿名函数
students.sort(key=lambda stu_dict:stu_dict['score'],reverse=True)
print(students)
a.录入学生作业情况:字典添加
b.查看学生作业情况:字典查询
c.录入时允许输入3次,3次输入不正确提示失败次数过多,禁止继续录入
homeworks = {
'Amy':{'2018.3.22':'未交','2018.3.23':'已交'},
'Lily':{'2018.3.22':'未交','2018.3.23':'未交'},
}
chioce = {'1':'查询','2':'录入'} #定义字典,可以避免输入‘0’或‘1’以外的选择时报错
#user_chioce input是字符串,如果用int转化非数字的字符串会报错,所以将key定义为字符串‘1’,而非数字1
user_chioce = input('请输入您的选择:1为查询学生作业情况,2为录入学生作业情况').strip()
if chioce.get(user_chioce) == '查询': #用get方法,输入key不在字典中不会报错。用chioce[user_choice],输入key不在字典中会报错
name = input('请输入要查询的学生姓名').strip()
if name == '':
print('查询学生姓名不能为空')
elif name in homeworks: #判断key是否在字典中存在
print('%s的作业情况是%s'%(name,homeworks[name]))#字典查询
else:
print('查询学生不存在')
elif chioce.get(user_chioce) == '录入':
state = {'0':'未交','1':'已交'} #定义字典,可以避免输入‘0’或‘1’以外的选择时报错
#放在循坏外,避免多次初始化,优化性能
for i in range(3):
enter_name = input('请输入要录入的学生姓名').strip()
enter_date = input('请输入要录入学生作业的日期').strip()
enter_state = input('请输入学生作业状态:0为未交,1为已交').strip()
if state.get(enter_state): #只要有状态就可以,不需要判断状态等于‘已交’或‘未交’
if enter_name == '' or enter_date == '':
print('学生姓名及作业日期不能为空')
else:
if enter_name in homeworks:
homeworks[enter_name].update({enter_date:state[enter_state]})#学生已存在,更新子字典
else:
homeworks[enter_name] = {enter_date:state[enter_state]}#学生不存在,直接添加新k-v
print('%s的作业情况是%s'%(enter_name,homeworks[enter_name]))
print('所有学生作业情况为%s' % homeworks)
break
else:
print('输入学生状态有误')
else:
print('选择输入有误')