why:
列表可以存储大量的数据,数据之间的关联性不强
[‘太白‘,18,‘男‘,‘大壮‘,3,‘男‘]
列表的查询速度比较慢
容器型数据类型:dict
how:
数据类型的分类(可变与不可变):
可变(不可哈希)的数据类型:list、dict、set
不可变(可哈希)的数据类型:str、bool、int、tuple
字典:{}括起来,以键值对形式存储的容器型数据类型
dic = {‘太白‘:
{‘name‘: ‘太白金星‘,‘age‘: 18, ‘sex‘: ‘男‘},
‘python22期‘:
[‘朱光亚‘, ‘大壮‘, ‘雪飞‘, ‘岑哥‘],
}
键必须是不可变的数据类型:str、int (bool、tuple几乎不用)
值可以是任意数据类型,对象
字典在3.5版本之前(包括3.5)是无序的
字典在3.6版本会按照初次建立字典的顺序排列,学术上不认为是有序的
字典在3.7版本以后都是有序的
字典的优点:查询速度非常快,存储关联性的数据
字典的缺点:以空间换时间
字典的创建方式:(面试会考:三种创建字典的方式)
方式一:
# 方式一:
dic = dict(((‘one‘, 1), (‘two‘, 2), (‘three‘, 3)))
print(dic)
#结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
方式二:
# 方式二:
dic = dict(one=1, two=2, three=3)
print(dic)
#结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
方式三:
# 方式三:
dic = dict({‘one‘: 1, ‘two‘: 2, ‘three‘: 3})
print(dic)
#结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
验证字典的合法性:
dic = {[1,2,3]: ‘alex‘, 1: 666} # 键要不可变的数据类型,list为可变数据类型
print(dic)
dic = {1: ‘alex‘, 1: ‘太白‘, 2: ‘wusir‘} # 键要唯一
print(dic)
增:
1.直接增加:dic[‘sex‘] = ‘男‘
? 有则改之,无则增加
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
dic[‘sex‘] = ‘男‘
#{‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘], ‘sex‘: ‘男‘}
?
dic[‘age‘] = 23
# 改:{‘name‘: ‘太白‘, ‘age‘: 23, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
?
print(dic)
2.setdefault:设置默认值
? dic.setdefault()
? 有则不变,无则增加
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
dic.setdefault(‘hobby‘)
#{‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘], ‘hobby‘: None}
?
dic.setdefault(‘hobby‘, ‘球类运动‘)
#{‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘], ‘hobby‘: ‘球类运动‘}
?
dic.setdefault(‘age‘, 45)
#{‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
print(dic)
删:
1.pop:按照键删除键值对,有返回值。
? 若设置第二个参数,则字典中有无此键都不报错
? dic.pop()
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
dic.pop(‘age‘)
#{‘name‘: ‘太白‘, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
?
ret = dic.pop(‘age‘) #返回值是 18
ret = dic.pop(‘hobby‘,‘没有此键‘) #虽然没有hobby,但是不报错,结果是没有此键
print(ret)
print(dic)
2.clear:清空
? dic.clear()
3.del
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
del dic[‘age‘]
#{‘name‘: ‘太白‘, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
?
del dic[‘age1‘] #报错
print(dic)
改:
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
dic[‘name‘] = ‘alex‘
print(dic)
#{‘name‘: ‘alex‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
?
查:
1.直接查:(基本不用,了解)
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
?
print(dic[‘hobby_list‘])
?
[‘直男‘, ‘钢管‘, ‘开车‘]
?
print(dic[‘hobby_list1‘])
?
没有则报错
?
2.get
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
l1 = dic.get(‘hobby_list‘)#[‘直男‘, ‘钢管‘, ‘开车‘]
l1 = dic.get(‘hobby_list1‘,‘没有此键sb‘) # 可以设置返回值
print(l1)
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
print(dic.keys(),type(dic.keys()))
# dict_keys([‘name‘, ‘age‘, ‘hobby_list‘]) <class ‘dict_keys‘>
可以转化为列表:
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
print(list(dic.keys()))
#[‘name‘, ‘age‘, ‘hobby_list‘]
#就是把键拿出来,形成列表
#竖着一个个输出键
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
for key in dic.keys():
print(key)
#结果:
name
age
hobby_list
?
for key in dic:
print(key)
#结果:
name
age
hobby_list
? values() 所有的值
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
print(dic.values())
#dict_values([‘太白‘, 18, [‘直男‘, ‘钢管‘, ‘开车‘]])
print(list(dic.values()))
#[‘太白‘, 18, [‘直男‘, ‘钢管‘, ‘开车‘]]
#就是把值拿出来,形成列表
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
for value in dic.values():
print(value)
#结果:
太白
18
[‘直男‘, ‘钢管‘, ‘开车‘]
?
for value in dic:
print(value)
#结果:
太白
18
[‘直男‘, ‘钢管‘, ‘开车‘]
? items() 所有的键值对
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
print(dic.items())
#dict_items([(‘name‘, ‘太白‘), (‘age‘, 18), (‘hobby_list‘, [‘直男‘, ‘钢管‘, ‘开车‘])])
dic = {‘name‘: ‘太白‘, ‘age‘: 18, ‘hobby_list‘: [‘直男‘, ‘钢管‘, ‘开车‘]}
for key,value in dic.items():
print(key,value)
#结果:
name 太白
age 18
hobby_list [‘直男‘, ‘钢管‘, ‘开车‘]
#面试题:实现a,b值的互换
a = 18
b = 12
a,b = b,a
print(a,b)
练习题:
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}
# 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典
dic.setdefault(‘k4‘,‘v4‘)
print(dic)
?
# 请在修改字典中 "k1" 对应的值为 "alex",输出修改后的字典
dic[‘k1‘] = ‘alex‘
print(dic)
?
# 请在k3对应的值中追加一个元素 44,输出修改后的字典
dic[‘k3‘].append(44)
print(dic)
?
?
dic.get(‘k3‘).append(44)
print(dic)
?
# 请在k3对应的值的第 1 个位置插入个元素 18,输出修改后的字典
dic[‘k3‘].insert(0,18)
print(dic)
dic = {
‘name‘: ‘汪峰‘,
‘age‘: 48,
‘wife‘: [{‘name‘: ‘国际章‘, ‘age‘: 38},],
‘children‘: {‘girl_first‘: ‘小苹果‘,‘girl_second‘: ‘小怡‘,‘girl_three‘: ‘顶顶‘}
}
?
# 1. 获取汪峰的名字。
print(dic.get(‘name‘))
?
# 2.获取这个字典:{‘name‘:‘国际章‘,‘age‘:38}。
print( dic.get(‘wife‘)[0])
?
# 3. 获取汪峰妻子的名字。
l1 = dic.get(‘wife‘)
dic2 = l1[0]
print(dic2.get(‘name‘))
?
#第二种:
s = dic[‘wife‘][0][‘name‘]
print(s)
# 4. 获取汪峰的第三个孩子名字。
dic1 = dic.get(‘children‘)
print(dic1.get(‘girl_three‘))
?
#第二种:
print(dic.get(‘children‘).get(‘girl_second‘))
?
字典:查询速度快,数据的关联性强
键不可变的数据类型,(str、int),唯一
值:任意数据类型,对象
增删改查 ***
字典的嵌套 ***
原文:https://www.cnblogs.com/yangzm/p/10859715.html