字典的定义
infos = {'name':'张晓红','sex':'女','address':'上海','age':18}
stus = {"name":"张三","sex":"女"}
print(list(stus.values())) #结果输出:['女', '张三']
print(list(stus.keys())) #结果输出:['sex', 'name']
print(list(stus.items())) #结果输出:[('sex', '女'), ('name', '张三')]
字典实现账号注册
stus = {"name":"张三"}
stus.update({"sex": "男"})
print(stus) #结果输出:{'name': '张三', 'sex': '男'}
stus.update({"sex":"女"})
print(stus) #结果输出:{'name': '张三', 'sex': '女'}
d = {'a':68,'w':62,'c':91,'d':83,'e':93,'f':89,'g':67}
print(list(d.items())) #结果输出:[('w', 62), ('a', 68), ('f', 89), ('c', 91), ('g', 67), ('d', 83), ('e', 93)]
d2 = sorted(d.items(),key=lambda x:x[1]) #lambda x:x[1] 根据字典的value排序,lambda x:x[0] 根据字典的key排序
print(d2) #结果输出:[('w', 62), ('g', 67), ('a', 68), ('d', 83), ('f', 89), ('c', 91), ('e', 93)]
print(d2[:2]) #结果输出:[('w', 62), ('g', 67)] d3 = sorted(d.items(),key=lambda x:x[1],reverse=True)
print(d3) #结果输出:[('e', 93), ('c', 91), ('f', 89), ('d', 83), ('a', 68), ('g', 67), ('w', 62)]
print(d3[:2]) #结果输出:[('e', 93), ('c', 91)]