列表list[]
# list = [12,34,56,78]
# print(list)
"""
1.list可以保存同一类型的数据 或 不同类型的数据
2.list是有序的,所以可以通过[下标]访问元素
3.list保存重复的值
4.list是可变的,可以添加 删除元素
"""
#添加元素
#在list的末尾追加元素
# list.append(True)
# print(list)
#在指定下标处插入元素
# list.insert(0,"abc")
# print(list)
#删除元素
#删除指定元素
# list.remove("abc")
# print(list)
#删除指定下标的元素
# list.pop(1)
# print(list)
#pop不传参 默认是末尾 -1
# list.pop()
# print(list)
#可以用del删除指定下标的元素
#del list[1]
# #字典dict{} key-value
# info_dict = {
# "name":"tom",
# "age":18,
# "score":90
# }
#
# #追加key-value
# #key不存在是追加
# info_dict["hobby"] = ["篮球","电竞"]
# print(info_dict)
# #修改key-value
# #key存在是修改
# info_dict["name"] = "Jack"
# print(info_dict)
#
# #删除
# del info_dict["hobby"]
# print(info_dict)
# #删除
# info_dict.pop("hobby")
# print(info_dict)
#例题:
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.统计不及格学生的个数
# count = 0
# for stu in students:
# if stu["score"] < 60:
# count += 1
# print(f"不及格的人数是:{count}")
#
# # b.打印不及格学生的名字和对应的成绩
# for stu in students:
# if stu["score"] < 60:
# print(f"姓名:{stu['name']} 成绩:{stu['score']}")
#
# # c.统计未成年学生的个数
# count = 0
# for stu in students:
# if stu["age"] < 18:
# count += 1
# print(f"不及格的人数是:{count}")
#
# # d.打印手机尾号是8的学生的名字
# for stu in students: #遍历students字典
# if stu["tel"][-1] == "8":
# print(f"手机尾号是8的学生姓名是:{stu['name']}")
#
# # e.打印最高分和对应的学生的名字
# max = students[0]['score']
# for stu in students:
# if stu['score'] > max:
# max = stu['score']
# print(f"max = {max}")
#
# for stu in students:
# if stu['score'] == max:
# print(f"高分者--{stu['name']}")
"""
sort()方法语法:
list.sort(cmp=None, key=None, reverse=False)
cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
"""
# d.对students按分数 从高到低进行排序
def mycmp(stu):
return stu['score']
#students.sort(key = mycmp,reverse = True) #加上reverse = True降序
students.sort(key = lambda stu:stu['score'],reverse = True) #key = lambda stu:stu['score']也是降序
#lambda后面跟的是一个匿名函数
for stu in students:
print(stu)