字典
引入
字典是列表以外Python中使用最灵活的内置数据类型,是一种可变的数据类型。
和列表的区别:
- 列表有序,字典无序
- 列表通过索引(偏移)访问元素,字典通过(key)访问元素
字典格式:
dict = {key1:value1,key2:value2,...,keyn:valuen}
值可以取任意数据类型,但键不可以,必须为不可变数据类型,即key必须可哈希。
注意:
可哈希—>不可变
不可哈希—>可变
特点:
- key:value,键值对之间用“:”连接
- key:value组成字典的一个元素
- 键值对之间用逗号隔开
- 整个字典用花括号{}包裹
字典的创建
(1)空字典的创建
dict = {}
print(type(dict))
<class 'dict'>
(2)多元素字典的创建
dict = {"id":31902103,"name":"chh","age":18}
key1,key2,key3 = "id","name","age"
value1,value2,value3 = 31902103,"chh",18
dic = {key1:value1,key2:value2,key3:value3}
print(dic)
{'id': 31902103, 'name': 'chh', 'age': 18}
(3)强转
#强转
dic = dict(id = 31902103,name = "chh",age = 18)
print(dic)
{'id': 31902103, 'name': 'chh', 'age': 18}
#使用zip()函数
key = ("id","name","age")
value = (31902103,"chh",18)
tp = dict(zip(key,value))
print(tp)
{'id': 31902103, 'name': 'chh', 'age': 18}
(4)通过字典的fromkeys()方法创建字典
格式:
dict.fromkeys(seq,val = none)
- 创建并返回一个字典
- 以seq中的元素作为该字典的键
- val的值作为该字典中所有键对应的初始值,若不提供默认为none
dic = dict.fromkeys(["TOM","Jack","Lucy"],6666)
print(dic)
{'TOM': 6666, 'Jack': 6666, 'Lucy': 6666}
字典的常见操作
(1)增
- 直接添加键值对(dic[key] = value)
dic = {}
dic["id"] = 31902103
dic["name"] = "chh"
dic["age"] = 18
print(dic)
{'id': 31902103, 'name': 'chh', 'age': 18}
-
dict.update(dic)
-
把字典dic中的键值对(key:value)更新到dict中
-
两个字典的键完全不同,则将dic的键值对全部添加到dict中
-
如果存在相同的键,则更新dict中对应的值
dict = {} dic = {"id":31902103,"name":"chh","age":18} dict.update(dic) print(dict) {'id': 31902103, 'name': 'chh', 'age': 18}
-
(2)删
-
dict.pop()
-
格式:
dict.pop(key[,default])
-
如果key存在于dict中,删除并返回dict[key],如果不存在,给出默认值则返回默认值,未给出默认值,报错
-
dict = {"id":31902103,"name":"chh","age":18} print(dict.pop("age")) print(dict) 18 {'id': 31902103, 'name': 'chh'}
-
-
-
dict.popitem()
-
随机删除一对键值对,由于字典是无序的,故删除最后一组
-
有返回值,元组
-
dict = {"id":31902103,"name":"chh","age":18} print(dict.popitem()) print(dict) key,value = dict.popitem() print(key,value) ('age', 18) {'id': 31902103, 'name': 'chh'} name chh
-
-
del dict
-
dict = {"id":31902103,"name":"chh","age":18} del dict["id"] print(dict) {'name': 'chh', 'age': 18}
-
-
dict.clear()
dict = {"id":31902103,"name":"chh","age":18} print(dict.clear()) print(dict) {}
(3)改
- 直接修改
dic = {"id":31902103,"name":"chh","age":18}
dic["id"] = 31902099
print(dic)
{'id': 31902099, 'name': 'chh', 'age': 18}
- dict.setdefault()
- 键存在,不改动,返回字典中的值
- 不存在,在字典中添加相应的键值对,并返回对应的值
dic = {"id":31902103,"name":"chh","age":18}
dic.setdefault("name","Tom")
print(dic)
{'id': 31902103, 'name': 'chh', 'age': 18}
dic = {"id":31902103,"name":"chh","age":18}
dic.setdefault("job","teacher")
print(dic)
{'id': 31902103, 'name': 'chh', 'age': 18, 'job': 'teacher'}
(4)查
- 字典值的访问
- 直接通过字典的key访问value
dic = {"id":31902103,"name":"chh","age":18}
print(dic["id"]) #存在返回对应的值
print(dic["job"]) #不存在,直接报错
31902103
- dict.get()访问value
- 语法格式dict.get(key[,default])
- 不确定字典中是否存在某个键而又想对其进行操作,比如获取值—>get方法
- 当key不存在的时候,不会抛出异常,返回None
dic = {"id":31902103,"name":"chh","age":18}
print(dic.get("id",20190201))
31902103
dic = {"id":31902103,"name":"chh","age":18}
print(dic.get("job",20190201))
20190201
- 遍历
- dict.keys()
- dict.values()
- dict.items()
dic = {"id":31902103,"name":"chh","age":18}
print(dic.keys())
dict_keys(['id', 'name', 'age'])
dic = {"id":31902103,"name":"chh","age":18}
print(dic.values())
dict_values([31902103, 'chh', 18])
dic = {"id":31902103,"name":"chh","age":18}
print(dic.items())
dict_items([('id', 31902103), ('name', 'chh'), ('age', 18)])
dic = {"id":31902103,"name":"chh","age":18}
for key,value in dic.items():
print(key,":",value)
id : 31902103
name : chh
age : 18
- 0,None,空字符串,空列表,空元组,空字典在逻辑判断上均为False