字典是一个无需、键不重复、元素只能是键值对的可变的容器.
data = {"a":1,"b":2}
data = {"a":1,"b":1}
print(data)
#{'a': 2}
v = {}
v1 = dict()
data = {"a":1,"b":2}
info = {
"name":"Ken",
"age":24,
"hobby":["computer","music"]
}
字典中对键值得要求:
data_dict = {
"ken":24
True:5
123:5
(11,22,33):5
}
data_dict = {
1: 1,
True: 1
}
print(data_dict)
#{1: 1}
#用户列表
user_list = {"admin":123,"root":111}
user_list = [{"name":"adimin","pwd":123},{"name":"root","pwd":123}]
info = {
"name":"Ken",
"age":24,
"hobby":["computer","music"]
}
data = info.get("name")
print(data)
#Ken
data1 = info.get("email")#对于不存在的键,get最终得到的是None
print(data1)
#None
#案例
user_list = {
"Ken" : "123",
"Pitter":321
}
username = input("请输入用户名:")
password = input("请输入密码:")
mid_value = user_list.get(username)#获取对应键所对应的值
if mid_value == None:
print("用户不存在!")
else:
#print(mid_value)
if password == mid_value:
print("Login Successful!")
else:
print("Login failur!")
info = {
"name":"Ken",
"age":24,
"hobby":["computer","music"]
}
data = info.keys()
#dict_keys(['name', 'age', 'hobby'])
info = {
"name":"Ken",
"age":24,
"hobby":["computer","music"]
}
for item in info.keys():
print(item)
info = {
"name":"Ken",
"age":24,
"hobby":["computer","music"]
}
data = info.values()
print(data1)
#dict_values(['Ken', 24, ['computer', 'music']])
for item in info.values():
print(item)
#Ken
#24
#['computer', 'music']
for key,value in info.items():
print(key,value)
#name Ken
#age 24
#hobby ['computer', 'music']
dict_list = {"name":"张三","Tall":180}
dict_list.setdefault("age",24)
print(dict_list)
dict_list.setdefault("name","GG")
print(dict_list)
#{'name': '张三', 'Tall': 180, 'age': 24}
#{'name': '张三', 'Tall': 180, 'age': 24}
由上我们可以知道:
dict_list = {"name":"张三","Tall":180}
print(dict_list)
dict_list.update({"name":"李四","Tall":182,"age":18})
print(dict_list)
#{'name': '张三', 'Tall': 180}
#{'name': '李四', 'Tall': 182, 'age': 18}
通过观察上方代码我们可以知道:
dict_list.update({"name":"李四","Tall":182,"age":18})
dict_list.pop("age")
print(dict_list)
#{'name': '张三', 'Tall': 180}
观察代码可知:
dict_list.update({"name":"李四","Tall":182,"age":18})
print(dict_list)
dict_list.popitem()
print(dict_list)
dict_list.popitem()
print(dict_list)
dict_list.update({"age":18})
print(dict_list)
dict_list.popitem()
print(dict_list)
# {'name': '李四', 'Tall': 182, 'age': 18}
# {'name': '李四', 'Tall': 182}
# {'name': '李四'}
# {'name': '李四', 'age': 18}
# {'name': '李四'}
观察可知:
另外:
#练习题
header = ['股票名称', '当前价', '涨跌额']
stock_dict = {
'SH601778': ['中国晶科', '6.29', '+1.92'],
'SH688566': ['吉贝尔', '52.66', '+6.96'],
'SH688268': ['华特气体', '88.80', '+11.72'],
'SH600734': ['实达集团', '2.60', '+0.24']
}
for key,value in stock_dict.items():
#print(key,value)
text_list = []
for index in range(len(value)):
text = "{}:{}".format(header[index],value[index])
text_list.append(text)
data = "、".join(text_list)
#print(data)
result = "{},{}.".format(key,data)
print(result)
# SH601778,股票名称:中国晶科、当前价:6.29、涨跌额:+1.92.
# SH688566,股票名称:吉贝尔、当前价:52.66、涨跌额:+6.96.
# SH688268,股票名称:华特气体、当前价:88.80、涨跌额:+11.72.
# SH600734,股票名称:实达集团、当前价:2.60、涨跌额:+0.24.
v1 = {"a":1,"b":2}
v2 = {"b":21,"c":2}
v3 = v1 | v2
print(v3)
#{'a': 1, 'b': 21, 'c': 2}
v1 = {"a":1,"b":2}
v2 = {"b":21,"c":2}
v3 = v1 | v2
print(v3)
print(len(v3))
#3
dict_list = {'name': '李四', 'Tall': 182, 'age': 18}
v1 = "age" in dict_list
print(v1)
v2 = "age" in dict_list.keys()
print(v2)
#True
True
不同于元组和列表,字典的索引是键
dict_list = {'name': '李四', 'Tall': 182, 'age': 18}
print(dict_list['name'])
print(dict_list['Tall'])
print(dict_list['age'])
#李四
#182
#18
dict_list = {'name': '李四', 'Tall': 182, 'age': 18}
dict_list['name'] = "王二麻子"
dict_list["Hobby"] = {"football","Music"}
print(dict_list)
#{'name': '王二麻子', 'Tall': 182, 'age': 18, 'Hobby': {'Music', 'football'}}
由上我们可以观察到:
由于字典也属于是容器,内部可以包含多个键值对,可以通过循环对其中的:键、值、键值进行循环;
dict_list = {'name': '王二麻子', 'Tall': 182, 'age': 18, 'Hobby': {'Music', 'football'}}
for item in dict_list:
print(item)
# name
# Tall
# age
# Hobby
dict_list = {'name': '王二麻子', 'Tall': 182, 'age': 18, 'Hobby': {'Music', 'football'}}
#del dict_list['name']
#data = dict_list.pop("name")
print(dict_list)
print(data)
#若不存在便会报错
#del dict_list['name']
# KeyError: 'name'
#data = dict_list.pop("name")
#KeyError: 'name'
v1 = {{"k1","aa"},{"k2","bb"}}
v2 = {("k1","aa"),("k2","bb")}
print(dict(v1))
print(dict(v2))
#{'k1': 'aa', 'k2': 'bb'}
#{'k1': 'aa', 'k2': 'bb'}
dict_list = {'name': '王二麻子', 'Tall': 182, 'age': 18, 'Hobby': {'Music', 'football'}}
print(list(dict_list))
print(list(dict_list.keys()))
print(list(dict_list.values()))
print(list(dict_list.items()))
# ['name', 'Tall', 'age', 'Hobby']
# ['name', 'Tall', 'age', 'Hobby']
# ['王二麻子', 182, 18, {'football', 'Music'}]
# [('name', '王二麻子'), ('Tall', 182), ('age', 18), ('Hobby', {'football', 'Music'})]
dic = {
"name ": '汪峰',
'age': 48,
'wife': [{'name': '国际章', 'age': 38}, {'name': '李杰', 'age': 48}],
'children': ['第一个娃', '第二个娃']
}
# 1,获取汪峰的妻子名字
d1 = dic['wife'][0]['name']
print(d1)
# 2.获取汪峰的孩子们
d2 = dic['children']
print(d2)
# 3。获取汪峰的第一个孩子
d3 = dic['children'][0]
print(d3)
# 4。汪峰的媳妇姓名变更为章子怡
dic['wife'][0]["name"] = "章子怡"
print(dic)
# 5。汪峰再娶一任妻子
dic["wife"].append({"name": "铁锤", "age": 19})
print(dic)
# 6。给汪峰添加一个爱好:吹牛逼
dic['hobby'] = '吹牛逼'
print(dic)
# 7.删除汪峰的年龄
del dic['age']
#dic.pop('age')
print(dic)
# 国际章
# ['第一个娃', '第二个娃']
# 第一个娃
# {'name ': '汪峰', 'age': 48, 'wife': [{'name': '章子怡', 'age': 38}, {'name': '李杰', 'age': 48}], 'children': ['第一个娃', '第二个娃']}
# {'name ': '汪峰', 'age': 48, 'wife': [{'name': '章子怡', 'age': 38}, {'name': '李杰', 'age': 48}, {'name': '铁锤', 'age': 19}], 'children': ['第一个娃', '第二个娃']}
# {'name ': '汪峰', 'age': 48, 'wife': [{'name': '章子怡', 'age': 38}, {'name': '李杰', 'age': 48}, {'name': '铁锤', 'age': 19}], 'children': ['第一个娃', '第二个娃'], 'hobby': '吹牛逼'}
# {'name ': '汪峰', 'wife': [{'name': '章子怡', 'age': 38}, {'name': '李杰', 'age': 48}, {'name': '铁锤', 'age': 19}], 'children': ['第一个娃', '第二个娃'], 'hobby': '吹牛逼'}