字典的创建与定义
字典是一个无序的数据集合,使用print输出字典时
通常输出的顺序和定义的顺序不一致
users = ['user1','user2']
passwd = ['123','456']
print(zip(users,passwd))
print(list(zip(users,passwd)))
print(dict(zip(users,passwd)))
结果:
<zip object at 0x0000021F6A040848>
[('user1', '123'), ('user2', '456')]
{'user1': '123', 'user2': '456'}
<class 'dict'>
字典的创建
(1)创建空字典
s = {} ##定义字典
print(type(s))
(2)key-value 键值对 value值可以是任意数据类型
s = {
'linux':[100,99,88],
'westos':[190,564,645]
}
print(s)
print(type(s))
结果:
{'linux': [100, 99, 88], 'westos': [190, 564, 645]}
<class 'dict'>
工厂函数
d = dict()
print(d)
print(type(d))
{}
<class 'dict'>
d1 = dict(a=1,b=2)
print(d1)
print(type(d1))
{'a': 1, 'b': 2}
<class 'dict'>
字典的嵌套
students = {
'03113009':{
'name':'tom',
'age':18,
'score':80
},
'03113010':{
'name': 'laoli',
'age': 19,
'score':30
}
}
print(students['03113010']['name']) ##key值以及value值
结果:
laoli
所有的key和value值是一样的情况(使用.fromkey)
print({}.fromkeys({'1','2'},'03113009'))
结果:
{'2': '03113009', '1': '03113009'}
字典的特性
不支持索引、切片;并且字典的重复、连接是无意义的(因为key是唯一的,不能重复出现)
成员操作符:判断的是某个值是否为字典的key
d = {
'1':'a',
'2':'b'
}
print('1' in d)
print('1' not in d)
True
False
for循环:默认遍历字典的key值
d = {
'1':'a',
'2':'b'
}
for i in d:
print(i)
结果:
1
2
遍历字典
d = {
'1':'a',
'2':'b'
}
第一种:
for key in d:
print(key,d[key]) ##d[key]相当于索引对应指向的value值
第二种:
for key,value in d.items():
print(key,value)
结果:
1 a
2 b
字典的增加
增加一个元素
如果key值存在,则更新对应的value值
如果key值不存在,则添加对应的key-value值
service = {
'http':80,
'ftp':21,
'ssh':22
}
service['mysql'] = 3306
print(service)
service['http'] = 443
print(service)
结果:
{'http': 80, 'ftp': 21, 'ssh': 22, 'mysql': 3306}
{'http': 443, 'ftp': 21, 'ssh': 22, 'mysql': 3306}
添加多个key-value值
如果key值存在,则更新对应的value值
如果key值不存在,则添加对应的key-value值
service = {
'http':80,
'ftp':21,
'ssh':22,
'mysql': 3306
}
service_backup = {
'https':443,
'tomcat':8080,
'http':8080
}
service.update(service_backup)
print(service)
service.update(flask=9000,http=8000)
print(service)
结果:
{'http': 8080, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080}
{'http': 8000, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080, 'flask': 9000
setdefault添加key值
如果key值存在,不做修改
如果key值不存在,则添加对应的key-value
service = {
'http':80,
'ftp':21,
'ssh':22,
'mysql': 3306,
'tomcat': 8080,
'flask': 9000
}
service.setdefault('http',9090)
print(service)
service.setdefault('oracle',44575)
print(service)
结果:
{'http': 8000, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080, 'flask': 9000}
{'http': 8000, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080, 'flask’:9000,'oracle',44575}
字典的删除
del
service = {
'http':80,
'ftp':21,
'ssh':22
}
del service['http']
print(service)
{'ftp': 21, 'ssh': 22}
pop:删除指定key的key-value
如果key存在,删除,并且返回删除key对应的value
如果key不存在,报错
service = {
'http':80,
'ftp':21,
'ssh':22
}
item = service.pop('http')
print(item)
print(service)
结果:
80
{'ftp': 21, 'ssh': 22}
popitem:删除最后一个key-value值
service = {
'http':80,
'ftp':21,
'ssh':22
}
item = service.popitem()
print('删除的key-value对是:',item)
print(service)
结果:
删除的key-value对是: ('ssh', 22)
{'http': 80, 'ftp': 21}
clear:清空字典内容
service.clear()
print(service)
结果:
{}
字典的查看
查看字典的key值
service = {
'http':80,
'ftp':21,
'ssh':22
}
print(service.keys())
结果:
dict_keys(['http', 'ftp', 'ssh'])
查看字典的value值
service = {
'http':80,
'ftp':21,
'ssh':22
}
print(service.values())
结果:
dict_values([80, 21, 22])
通过key的“索引”性质 查看key的value值 key不存在,报错
service = {
'http':80,
'ftp':21,
'ssh':22
}
print(service['https']) ##会报错
get方法获取指定key对应的value值
如果key值存在,返回对应的value值
如果key值不存在,默认返回None,如果需要指定返回值,传值即可
service = {
'http':80,
'ftp':21,
'ssh':22
}
print(service.get('https',443))
print(service.get('https'))
结果:
443
None
需要指定返回值,传值即可
service = {
'http':80,
'ftp':21,
'ssh':22
}
第一种写法:
print(service.get('https','ket not exist'))
另一种写法:
print(service['https'])
if 'https' in service:
print(service['https'])
else:
print('key not exist')
结果:
key not exist
ket not exist
1
遍历
service = {
'http':80,
'ftp':21,
'ssh':22
}
for k,v in service.items():
print(k,'--->',v)
结果:
http ---> 80
ftp ---> 21
ssh ---> 22
本文深入讲解了Python中字典的创建、查看、增加、删除等操作,并提供了丰富的代码实例,帮助读者理解字典的特性和使用方法。
1717

被折叠的 条评论
为什么被折叠?



