1.字典的定义
字典是一个无序的数据集合,使用print输出字典时,通常输出的顺序和定义的顺序不一致
(1).将两个列表转换为一个字典类型
# 定义列表
users = ['user1','user2']
passwd = ['123','456']
print((users,passwd))
# zip: 重组
print(zip(users,passwd))
# tuple表示元组
print(type((users,passwd)))
# list: 强制转换为列表类型
print(list(zip(users,passwd)))
# dict: 强制转换为字典类型
print(dict(zip(users,passwd)))
(2).定义字典
字典: key-value (键值对), value值可以是任意数据类型
s = {'linux':[100,99,88],'westos':[120,241,532]}
print(s)
(3).定义空字典
方法1:
s = {}
print(s)
# dict表示字典
print(type(s))
方法2:
# dict:工厂函数
d = dict()
print(d)
print(type(d))
(4).工厂函数
d1 = dict(a=1,b=2)
print(d1)
print(type(d1))
(5).字典的嵌套
student = {'07161032':{'name':'tom','age':18,'score':99},
'07161017':{'name':'lily','age':16,'score':100}}
# 打印指定key的value值
print(student['07161032']['name'])
(6).定义具有相同value值的字典
print({}.fromkeys({'1','2','3'},'07161032'))
2.字典的特性
(1).成员操作符
# 定义字典
d = {'1':'a','2':'b'}
# 默认情况下指的是key值
print('1' in d)
# keys:key值
print('1' in d.keys())
# values: 键值
print('a' in d.values())
(2)for循环遍历(迭代)
遍历字典的key值:
# 定义字典
d = {'1':'a','2':'b'}
# 默认遍历字典的key值
for i in d:
print(i)
# keys:key值
for key in d.keys():
print(key)
遍历字典的value值:
# 定义字典
d = {'1':'a','2':'b'}
for value in d.values():
print(value)
遍历字典的key值和value值:
# 定义字典
d = {'1':'a','2':'b'}
# 遍历字典的key值和value值
for key,value in d.items():
print(key,value)
字典的重复和连接无意义,因为字典的key值是唯一的
字典不支持索引和切片
# 定义字典
d = {'1':'a','2':'b'}
# 字典不支持索引,切片
print(d[0])
print(d[:])
3.字典的增加
(1).添加一个key-value值
# 定义字典
service = {'http':80,'ftp':21,'ssh':22}
print(service)
# 若key值不存在,则添加对应的key-value值
service['mysql'] = 3306
print(service)
# 若key值存在,则更新对应的value值
service['http'] = 8080
print(service)
(2). update添加多个key-value值
update添加规则:
如果key值存在,则更新对应的value值
如果key值不存在,则添加对应的key-value值
方法1:
# 定义字典
service = {'http':80,'ftp':21,'ssh':22}
print(service)
addservice = {'https':443,'tomcat':8080,'http':8080}
service.update(addservice)
print(service)
方法2:
# 定义字典
service = {'http':80,'ftp':21,'ssh':22}
print(service)
service.update(nginx=80,http=8000)
print(service)
(3).setdefault添加key-value
setdefault添加规则:
如果key值存在,则不做修改
如果key值不存在,则添加对应的key-value
# 定义字典
service = {'http':80,'ftp':21,'ssh':22}
print(service)
# key值存在,不做修改
service.setdefault('http',9090)
print(service)
# key值不存在,则添加对应的key-value
service.setdefault('oracal',44575)
print(service)
4.字典的删除
(1)del 删除指定key的key-value
# 定义字典
service = {'http':80,'ftp':21,'ssh':22,'nginx':80}
print(service)
# del: 删除元素
del service['http']
print(service)
(2)pop 删除指定key的key-value
删除原则:
如果key存在,删除,并且返回删除key对应的value
如果key不存在,报错
# 定义字典
service = {'http':80,'ftp':21,'ssh':22,'nginx':80}
print(service)
item = service.pop('ssh')
print(item)
print(service)
(3)popitem 删除最后一个key-value值
3. popitem: 删除最后一个key-value值
item = service.popitem()
print('删除的key-value对是:',item)
print(service)
(4) clear 清空字典内容
service.clear()
print(service)
5.字典的查看
(1).查看字典
service = {'http':80,'ftp':21,'ssh':22}
# 查看字典
print(service)
(2). 查看字典的key值
service = {'http':80,'ftp':21,'ssh':22}
# 查看字典的key值
print(service.keys())
(3).查看字典的value值
service = {'http':80,'ftp':21,'ssh':22}
# 查看字典的value值
print(service.values())
(4). 查看指定key的value值
service = {'http':80,'ftp':21,'ssh':22}
# 查看指定key的value值
# 若key不存在,报错
print(service['http'])
# print(service['https'])
(5)get 方法获取指定key对应的value值
service = {'http':80,'ftp':21,'ssh':22}
# key不存在,默认返回None
# key不存在,有default,则返回default
print(service.get('http'))
print(service.get('https'))
print(service.get('https',443))
# 如果key值存在,返回对应的value值
# 如果key值不存在,默认返回None,如果需要指定返回值,传值即可
print(service.get('http','key not exist'))
print(service.get('https','key not exist'))
(6)for循环遍历(迭代)
service = {'http':80,'ftp':21,'ssh':22}
for k,v in service.items():
print(k,'--->',v)
(7)判断指定的key是否存在
if 'https' in service:
print(service['https'])
else:
print('key not exist')