Python入门之字典dict详细讲解

雨点打透心脏的1/2处 2021-12-15 00:15 589阅读 0赞

一、字典的定义

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

  1. 字典的创建

    users = [‘user1’,’user2’]
    passwd = [‘123’,’456’]
    print(zip(users,passwd))
    print(list(zip(users,passwd)))
    print(dict(zip(users,passwd)))

20190704100428990.png

2.空字典的定义

  1. s = {} #空字典
  2. print(type(s))
  3. d = dict() #空字典
  4. print(d,type(d))

20190704100524585.png

  1. 字典格式:key-value键值对

字典的key值是唯一的

  1. s = {
  2. 'westos':[190,521,231],
  3. 'mysql':[100,99,88]
  4. }
  5. print(s,type(s))

20190704100625387.png

  1. 工厂函数

    d1 = dict(a=1,b=2)
    print(d1,type(d1))

20190704102135861.png

5.嵌套

  1. students = {
  2. '03113009':{
  3. 'name':'laoli',
  4. 'age':39,
  5. 'score':59
  6. },
  7. '03113010':{
  8. 'name':'westos',
  9. 'age':18,
  10. 'score':61
  11. }
  12. }
  13. print(students['03113009']['name'])
  14. print(students['03113010'])

20190704102246766.png

6.分批赋相同的value值

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

20190704102729487.png

二、字典的特性

字典不支持索引和切片,字典的重复和连接也是无意义的。

  1. 成员操作符

    d = {

    1. '1':'a',
    2. '2':'b'

    }
    print(‘1’ in d) #对key进行判定
    print(‘1’ not in d)

20190704193011461.png

  1. for循环(遍历字典的key值)

    d = {

    1. '1':'a',
    2. '2':'b'

    }
    for key in d:

    1. print(key)

20190704193142908.png

遍历字典

  1. d = {
  2. '1':'a',
  3. '2':'b'
  4. }
  5. for key in d: #与下一个作用类似
  6. print(key,d[key])
  7. for k,v in d.items():
  8. print(k,v)

20190704193256264.png

三、字典的增加

  1. 添加不存在的项

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services)
    services[‘ftp’] = 21
    print(services)

20190704193355785.png

  1. 添加已经存在的项,会更改值

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services)
    services[‘http’] = 443
    print(services)

20190704193624262.png

  1. 添加多个key-value值

如果添加的key-value存在,则只改变键值;不存在的项会直接添加

  1. services = {
  2. 'http':80,
  3. 'mysql':3306,
  4. 'smtp':25
  5. }
  6. services_backup = {
  7. 'https':443,
  8. 'tomcat':8080,
  9. 'http':8080
  10. }
  11. print(services)
  12. services.update(services_backup)
  13. print(services)
  14. services.update(flask=9000,http=8000)
  15. print(services)

20190704193847409.png

  1. setdefault添加key值

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

  1. services = {
  2. 'http':80,
  3. 'mysql':3306,
  4. 'smtp':25
  5. }
  6. print(services)
  7. services.setdefault('http',9090)
  8. print(services)
  9. services.setdefault('oracle',44575)
  10. print(services)

20190704194348100.png

四、字典的删除

  1. del删除指定的key-value键值对

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services)
    del services[‘http’]
    print(services)

20190704194501504.png

  1. pop删除指定的key的key-value

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

  1. services = {
  2. 'http':80,
  3. 'mysql':3306,
  4. 'smtp':25
  5. }
  6. item = services.pop('http')
  7. print(item)
  8. print(services)

20190704195021236.png

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

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services)
    item = services.popitem()
    print(services)

20190704195255797.png

  1. clear清空字典

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    services.clear()
    print(services)

    20190704195410510.png

五、字典的查看

  1. 查看字典的key值

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services.keys())

    20190704195551803.png

  2. 查看字典的value值

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services.values())

20190704195643888.png

  1. 查看字典的key-value值

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    print(services.items())

20190704195733304.png

  1. 查看key的value值

key不存在,默认返回None;key不存在,有defaulte,则返回defaulte值

  1. services = {
  2. 'http':80,
  3. 'mysql':3306,
  4. 'smtp':25
  5. }
  6. print(services.get('https'))
  7. print(services.get('http'))

20190704195820926.png

  1. for迭代

    services = {

    1. 'http':80,
    2. 'mysql':3306,
    3. 'smtp':25

    }
    for k in services:

    1. print(k,services[k])

2019070419591194.png

  1. get方法

如果key值存在,返回;如果不存在,默认返回None,如果需要指定返回值,传值即可

  1. services = {
  2. 'http':80,
  3. 'mysql':3306,
  4. 'smtp':25
  5. }
  6. print(services.get('https','key not exist'))
  7. print(services.get('mysql','key not exist'))

20190704200023906.png

发表评论

表情:
评论列表 (有 0 条评论,589人围观)

还没有评论,来说两句吧...

相关阅读

    相关 pythondict字典

    字典是一系列由键(key)和值(value)配对组成的元素的集合,在 Python3.7+,字典被确定为有序(注意:在 3.6 中,字典有序是一个implementation

    相关 Python入门系列8:dict( 字典)

    字典也是 python 提供给我们的又一个非常重要且有用的数据结构。 字典在别的语言中有时叫关联数组、关联内存、Map等。 字典中存储的是一系列的`key-value`,这