7.19字典
作者:互联网
文章目录
1.假如有一个列表
Li = [“Pyth”]
2.假如统计学生信息,包括学号,姓名,年龄
li = [[20190101,“Tom”,18],
[20190201,“Jack”,19],
…,
[20190n01,“LiLei”,18]]
循环遍历,十分复杂
考虑字典
info = {"name": "Tom", "age": 18, "id": 20190101}
# key : value --->键值对
print(info)
{'name': 'Tom', 'age': 18, 'id': 20190101}
字典是列表以外Python中使用最灵活的内置数据类型
字典是一种可变的数据类型。
和列表的区别
- 列表有序,字典无序
- 列表通过索引(偏移)访问,字典是通过键(key)访问元素
字典的基本格式:
dic = {key1:value,key2:value2,...,keyn:valuen}
值可以去任意数据类型,但键不可以,必须为不可变数据类型,即key可哈希。
可哈希—>不可变
不可哈希—>可变
特点:
- 1.key:value,键值之间用冒号(:)连接
- 2.key:value组成字典的一个元素
- 3.键值对之间用逗号(,)隔开
- 4.整个字典用花括号({})包裹
字典的创建
空字典
dic = {}
print(type(dic)
<class 'dict'>
创建多元素字典
info = {"name": "Tom", "age": 18, "id": 20190101}
# key : value --->键值对
print(info)
{'name': 'Tom', 'age': 18, 'id': 20190101}
因为字典是无序的所以只能通过键名来查
强转
dic = dict(id=20190101, name="Tom", age=18)
print(dic)
zip()
key = ("id", "name", "age")
value = (20190101, "Tom", 18)
tp = dict(list(zip(key, value)))
print(tp, type(tp))
{'id': 20190101, 'name': 'Tom', 'age': 18}
{'id': 20190101, 'name': 'Tom', 'age': 18} <class 'dict'>
通过字典的fromkeys方法创建字典
格式:
dict.fromkeys(seq,val=None)
- 创建并返回一个字典
- 以seq中的元素作为该字典的键
- val的值作为该字典中所有键对应的初始值:若不提供,默认为None
dic = dict.fromkeys("Hello")
dic1 = dict.fromkeys("Tom", "Jack")
print(dic)
print(dic1)
{'H': None, 'e': None, 'l': None, 'o': None}
{'T': 'Jack', 'o': 'Jack', 'm': 'Jack'}
字典的常见操作
增
-
直接添加键值(dic[key]=value)
- 通过使用变量名[“键”] = “值”,
- 如果key不存在,新建键值对(key:value)
- 如果存在,则更新值(value)
- 通过使用变量名[“键”] = “值”,
-
dic = {} dic["id"] = 20190101 dic["name"] = "Tom" dic["age"] = 19 print(dic) {'id': 20190101, 'name': 'Tom', 'age': 19}
-
dict.update(dic)
把字典dic中的键值对(key:value)更新到dict中
-
两个字典的键完全不同,则将dic的键值对全部添加至dict中
-
存在相同的键,则更新dict中对应的值
-
dic = {"job": "IT", "hobby": "Read"} dic2 = {"name": "Jack", "hobby": "Read"} dict = {"id": 20190101, "name": "Tom", "age": 22} dict.update(dic2) print(dict) {'id': 20190101, 'name': 'Jack', 'age': 22, 'hobby': 'Read'}
-
删
-
dict.pop
-
格式:dict.pop(key[,default])
-
如果key存在于dict中,删除并返回dict[key]
-
如果不存在,给出默认值则返回默认值,未给出默认值,报错
-
dict = {"id": 20190101, "name": "Tom", "age": 22} print(dict.pop("job", "it")) print(dict)
-
-
-
Dict.popitem()
-
随机删除一对键对
-
dict = {"id": 20190101, "name": "Tom", "age": 22} print(dict.popitem()) print(dict) key, value = dict.popitem() print(dict) print(key, value) ('age', 22) {'id': 20190101, 'name': 'Tom'} {'id': 20190101} name Tom
-
-
Dict.clear()
-
dict = {"id": 20190101, "name": "Tom", "age": 22} print(dict.clear()) None
-
-
Del dict[key]关键字
-
dict = {"id": 20190101, "name": "Tom", "age": 22} del dict["age"] print(dict) {'id': 20190101, 'name': 'Tom'}
-
改
-
直接修改
-
dic = {"id": 20190101, "name": "Tom", "age": 22} dic["id"] = 20190201 print(dic) {'id': 20190201, 'name': 'Tom', 'age': 22}
-
dict.setdefault()
-
键存在,不改动,返回字典中的值
-
不存在,在字典中添加相应的键值对,并返回对应的值
-
dic = {"id": 20190101, "name": "Tom", "age": 22} print(dic.setdefault("name", "Jack")) # 键存在 print(dic) print(dic.setdefault("salary", "Jack")) # 不存在 print(dic) Tom {'id': 20190101, 'name': 'Tom', 'age': 22} Jack {'id': 20190101, 'name': 'Tom', 'age': 22, 'salary': 'Jack'}
-
查
-
字典值的访问
-
dic = {"id": 20190101, "name": "Tom", "age": 22} print(dic["id"])#存在返回对应的值 print(dic["job"])#不存在会直接报错 20190101
若访问不存在的值会报错
-
Dict.get()访问value
-
语法格式dict.get(key[,default])
-
不确定字典中是否存在某个键而又想对其进行操作,比如获取值—>get()方法
-
当key不存在的时候,不会抛出异常,返回None
-
dic = {"id": 20190101, "name": "Tom", "age": 22} print(dic.get("name")) Tom
-
dic = {"id": 20190101, "name": "Tom", "age": 22}
print(dic.get("nam"))
None
dic = {"id": 20190101, "name": "Tom", "age": 22}
print(dic.get("nam","23"))
23
-
遍历
-
Dict.keys()
-
dic = {"id": 20190101, "name": "Tom", "age": 22} print(dic.keys(),type(dic.keys())) dict_keys(['id', 'name', 'age']) <class 'dict_keys'>
-
dic = {"id": 20190101, "name": "Tom", "age": 22} print(dic.keys(), type(dic.keys())) for key in dic.keys(): print(key, end=" ") dict_keys(['id', 'name', 'age']) <class 'dict_keys'> id name age
-
Dict.values()
-
dic = {"id": 20190101, "name": "Tom", "age": 22} for value in dic.values(): print(value,end=" ") 20190101 Tom 22
-
Dict.items()
-
dic = {"id": 20190101, "name": "Tom", "age": 22} for item in dic.items(): print(item,end=" ") ('id', 20190101) ('name', 'Tom') ('age', 22)
-
标签:name,age,7.19,dic,20190101,Tom,id,字典 来源: https://blog.csdn.net/weixin_44314681/article/details/96485094