Pyhon入门之字典(dict)详解

1 字典的定义创建

1.1 字典的定义:

字典是另一种可变数据类型,可存储任意类型对象。
无序的序列,键值对的输入顺序和在内存中的存储顺序不一致
字典中的数据存储以键值对的方式
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中
1.2 字典的创建:key-value键值对(value值可以是任意数据类型)

s = {
    'linux':[100,99,88],
    'python':[190,55,79]
}
print(s,type(s))

1.3 工厂函数创建字典:

d = dict()
print(d,type(d))
c = dict(a=1,d=2)
print(c,type(c))
{} <class 'dict'>
{'a': 1, 'd': 2} <class 'dict'>

1.4 创建空字典:

d = dict()
c = {}
print(d,type(d))
print(c,type(c))
{} <class 'dict'>
{} <class 'dict'>

1.5 字典的嵌套:

#字典的嵌套
students = {
    '03113009':{
        'name':'laoli',
        'age':39,
        'score':59
    },
    '03113010':{
        'name':'westos',
        'age':18,
        'score':61
    }
}
print(students['03113009']['name'])

结果:
laoli

1.6 所有的key和value值是一样的情况:

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

结果:
{'1': '000000', '2': '000000'}

2 字典的特性

注:字典不支持索引,切片;字典的重复和连接无意义,因为字典的key值是唯一的
1.通过key值,输出value值

a = {                                                                               
    'name':'westos',                                                                
    'age' :18                                                                       
}                                                                                   
print(a['name'])     

输出:westos

2.成员操作符

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

print('1' in d)
print('1' not in d)

结果:
True
False

3.for循环,默认遍历字典的key值

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

for key in d:
    print(key)

结果:
1
2

4.遍历字典

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

#方法一
for key in d:
    print(key,d[key])
#方法二
for key,value in d.items():
    print(key,value)

结果:
1 a
2 b
1 a
2 b

3 字典的增加

1.增加一个元素:如果key值存在,则更新对应的value值,如果key值不存在,则添加对应的key-value值

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

services['mysql'] = 3306
print(services)
services['http'] = 443
print(services)

结果:
{'http': 80, 'ftp': 21, 'ssh': 22, 'mysql': 3306}
{'http': 443, 'ftp': 21, 'ssh': 22, 'mysql': 3306}

2.添加多个key-value值:如果key值存在,则更新对应的value值,如果key值不存在,则添加对应的key-value值

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

services_backup = {
    'https':443,
    'tomcat':8080,
    'http':8080
}
services.update(services_backup)
print(services)

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

结果:
{'http': 8080, 'ftp': 21, 'ssh': 22, 'https': 443, 'tomcat': 8080}
{'http': 8000, 'ftp': 21, 'ssh': 22, 'https': 443, 'tomcat': 8080, 'flask': 9000}

3.setdefault添加key值:如果key值存在,不做修改,如果key值不存在,则添加对应的key-value

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}
services.setdefault('http',9090)
print(services)
services.setdefault('oracle',44575)
print(services)

结果:
{'http': 80, 'ftp': 21, 'ssh': 22}
{'http': 80, 'ftp': 21, 'ssh': 22, 'oracle': 44575}

四.字典的删除

1.del删除

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

del services['http']
print(services)

结果:
{'ftp': 21, 'ssh': 22}

2.pop删除指定key的key-value:如果key存在,删除,并且返回删除key对应的value,如果key不存在,报错

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

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

结果:
80
{'ftp': 21, 'ssh': 22}

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

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

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

结果:
删除的是: ('ssh', 22)
{'http': 80, 'ftp': 21}

4.清空字典内容

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

services.clear()
print(services)

结果:
{}

五.字典的查看

1.查看字典的key值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}
print(services.keys())

结果:
dict_keys(['http', 'ftp', 'ssh'])

2.查看字典的value值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}
print(services.values())

结果:
dict_values([80, 21, 22])

3.查看字典的key-value值

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}
print(services.items())

结果:
dict_items([('http', 80), ('ftp', 21), ('ssh', 22)])

4.get方法获取指定key对应的value值:如果key值存在,返回对应的value值,如果key值不存在,默认返回None,如果需要指定返回值,传值即可

services = {
    'http':80,
    'ftp':21,
    'ssh':22
}
print(services.get('https','key not exist!'))

结果:
key not exist!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值