键值对组成的可变映射容器(可变)
映射:一对一的对应关系,曲每条记录无序
键必须唯一且不可变,值没有限制
创建字典
#创建字典
dict01={}
dict01=dict()
#默认值
dict01={"wj":100,"zm":80,"zr":90}
dict02=dict([("a","b"),("c","d")])
#查找元素(根据key查找value)
print(dict01["a"])
#修改元素
dict101['a'] = "BB"
#添加
dict101['e'] = "f"
#删除
del dict01['a']
#遍历字典得到的是key
for item in dict01:
print(dict01[item])
#遍历字典得到的是value
for value in dict01.values():
print(value)
#遍历字典得到的键值对 key value
for value in dict01.items():
print(item[0])
print(item[1])
for k,y in dict01.items():
print(k)
print(y)
dict01 = {}
while True:
name = input("请输入商品名称")
if name == "":
break
price = input("请输入单价:")
if name not in dict01:
dict01[name] = price
for key, value in dict01.items():
print(key + "=>" + value)
dict01 = {}
while True:
name = input("请输入学生姓名")
if name == "":
break
age = input("请输入年龄:")
score = input("请输入成绩:")
sex = input("请输入性别:")
if name not in dict01:
dict01[name] = [age, score, sex]
print(dict01)
for key, value in dict01.items():
print("%s性别%s%s成绩为%s" % (key, value[2], value[0], value[1]))
dict01 = {}
while True:
name = input("请输入学生姓名")
if name == "":
break
age = input("请输入年龄:")
score = input("请输入成绩:")
sex = input("请输入性别:")
if name not in dict01:
dict01[name] = {"age":age, "score":score, "sex":sex}
print(dict01)
for key, value in dict01.items():
print("%s性别%s%s成绩为%s" % (key, value["sex"], value["age"], value["score"]))
list_peron =[]
while True:
name = input("请输入姓名:")
if name == "":
break
dict01={name:[]}
list_peron.append(dict01)
while True:
hbit = input("请输入喜好:")
if hbit == "":
break
dict01[name].append(hbit)
for item in list_peron:
print(item)
list_peron ={}
while True:
name = input("请输入姓名:")
if name == "":
break
list_peron[name] = []
while True:
hbit = input("请输入喜好:")
if hbit == "":
break
list_peron[name].append(hbit)
for item in list_peron.items():
print(item)
list01=[item for item in range(1970,2051) if item % 4 == 0 and item % 100 != 0 or item % 400 == 0 ]
dict01 = {
"北京": {
"景区": ["故宫", "天安门", "天坛"],
"美食": ["烤鸭", "炸酱面", "豆汁"]
},
"四川": {
"景区": ["九寨沟", "峨眉山", "春熙路"],
"美食": ["火锅", "串串香", "兔头"]
}
}
print(dict01['四川']['美食'])
dict01 = {}
str_input = "abcdefce"
for i in str_input:
if i in dict01:
dict01[i] += 1
else:
dict01[i] = 1
for key, value in dict01.items():
print("%s : %d" % (key, value))



