您好,欢迎来到汇智旅游网。
搜索
您的当前位置:首页Python之字典的详细介绍及使用示例

Python之字典的详细介绍及使用示例

来源:汇智旅游网

一、字典的定义

字典是一个无序的数据集合,使用print输出字典的时候
通常输出的顺序和定义的顺序是不一致的

字典:key - value 键值对
value可以是任意数据类型

s = {
    'linux':[100,99,88],
    'westos':[190,5,5]
}
print(s,type(s))

1、工厂函数

d = dict()			#定义空字典
print(type(d))

d1 = dict(a=1,b=2)	#工厂函数
print(d1,type(d1))

2、字典的嵌套

students = {
    '03113009':{
        'name':'laozhao',
        'age':18,
        'score':80
    },
    '03113010':{
        'name': 'laoyan',
        'age': 30,
        'score': 59
    }
}

print(students['03113010']['age'])

3、定义一个字典的所有key对应的value值是一样的

print({}.fromkeys({'1','2'},'000000'))

二、字典的特性

  • 字典不支持索引
  • 字典不支持切片
  • 字典的重复和连接无意义

1、成员操作符

d = {
    '1':'a',
    '2':'b'
}

print('2' in d)

2、for循环,默认遍历字典的key值

d = {
    '1':'a',
    '2':'b'
}

for i in d:
    print(i)

d = {
    '1':'a',
    '2':'b'
}

for i in d:
    print(i,d[i])
    
for k,v in d.items():		#遍历key,value值
    print(k,':',v)

三、字典的增加

1、添加一个key-value值

如果key值存在,则更新对应的value值
如果key值不存在,则添加对应key-value

示例:

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

services['mysql'] = 3306		#不存在,添加对应key-value
print(services)

services['http'] = 443			#存在,则更新对应的value值
print(services)

2、添加多个key-value值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}
services_backup = {
    'https':443,
    'tomcat':8080,
    'http':8080
}

services.update(services_backup)
print(services)

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

services.update(flask=9000,http=8000)
print(services)

3、setdefault添加key值

如果key值存在,不做修改
如果key值不存在,添加对应的key-value

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

services.setdefault('http',9090)
print(services)

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

services.setdefault('oracle',44575)
print(services)

四、字典的删除

1、del

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

del services['http']
print(services)

2、pop删除指定key的key-value

如果key存在,删除,并返回删除key对应的value
如果不存在,报错

示例:

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

item = services.pop('http')
print(item)
print(services)

3、popitem删除最后一个key-value值

示例:

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

item = services.popitem()
print('删除的是:',item)
print(services)

4、清空字典内容

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

services.clear()
print(services)

五、字典的查看

1、查看字典的key值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

print(services.keys())

2、查看字典的value值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

print(services.values())

3、查看字典的key-value值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

print(services.items())

4、查看key的value值

key不存在,默认返回none
key不存在,有default值,则返回default值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

print(services.get('http'))

5、for循环(迭代)

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

for k,v in services.items():
    print(k,'--->',v)

for k in services:
    print(k,'--->',services[k])

6、get方法获取指定key对应的value

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}

print(services.get('https','key not exist'))

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- hzar.cn 版权所有 赣ICP备2024042791号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务