zoukankan      html  css  js  c++  java
  • 字典数据类型内置方法

    字典数据类型内置方法

    1.作用

    用于对值添加描述信息

    2.定义方式

      #用{}以逗号隔开加入键值对key:value
      info_dict={'name':'bb','age':18,'height':180,'weight':140,'gender':'male','hobby_lsit':['13','666','233']}
    

    3.内置方法

    优先掌握

    1.按key取值,即可取也可改变

    info_dict = {'name': 'bb', 'age': 18, 'height': 180, 'weight': 140, 'gender': 'male', 'hobby_list': ['13', '666','233']}
    print(info_dict['hobby_list'])
    info_dict['age']=20
    print(info_dict)
    

    ['13', '666', '233']
    {'name': 'bb', 'age': 20, 'height': 180, 'weight': 140, 'gender': 'male', 'hobby_list': ['13', '666', '233']}

    2.长度len

    print(len(info_dict))
    

    6

    3.in/not in

    print('name' in info_dict)
    

    True

    4.for循环

    for i in info_dict:
        print(i)
    
    

    name
    age
    height
    weight
    gender
    hobby_list

    5.键keys()、值values()、键值对items() items用得最多一般和解压缩一起用

    print(list(info_dict.keys()))  # list
    print(list(info_dict.values()))  # list
    print(list(info_dict.items()))
    

    ['name', 'age', 'height', 'weight', 'gender', 'hobby_list']
    ['bb', 18, 180, 140, 'male', ['13', '666', '233']]
    [('name', 'bb'), ('age', 18), ('height', 180), ('weight', 140), ('gender', 'male'), ('hobby_list', ['13', '666', '233'])]

    6.循环

    info_dict = {'name': 'bb', 'age': 18, 'height': 180, 'weight': 140, 'gender': 'male', 'hobby_lsit': ['13', '666','233']}
    for i in info_dict.items():
        print(i)
    
    for k, v in info_dict.items():
        print(k, v)
    lt = [[1, 2, 3], [2, 3, 4], [5, 6, 7]]
    for a, b, c in lt:
        print(a, b, c)
    

    ('name', 'bb')
    ('age', 18)
    ('height', 180)
    ('weight', 140)
    ('gender', 'male')
    ('hobby_lsit', ['13', '666', '233'])
    name bb
    age 18
    height 180
    weight 140
    gender male
    hobby_lsit ['13', '666', '233']
    1 2 3
    2 3 4
    5 6 7

    需要掌握

    1.copy/pop/popitem/clear

    info_dict = {'name': 'wangdapao', 'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233']}
    print(info_dict.copy())
    
    info_dict.pop('name')
    
    print(info_dict)
    
    info_dict.popitem()  # 早期的时候字典是无序的,是随机删除的,但是由于python3底层优化了字典,让字典看的貌似有序了,所以默认删除最后一个
    print(info_dict)
    
    info_dict.clear()
    print(info_dict)
    

    {'name': 'wangdapao', 'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233']}
    {'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233']}
    {'age': 18, 'height': 120, 'gender': 'female'}
    {}

    2.get

    print(info_dict.get('age'))
    # print(info_dict['name'])
    print(info_dict.get('name', 'nick'))  # 如果有,就返回真正的值;如果没有,默认返回None,也可以指定返回
    
    

    18
    wangdapao

    3.update

    info_dict = {'name': 'wangdapao', 'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233']}
    
    info_dict.update({'a':1})
    print(info_dict)
    

    {'name': 'wangdapao', 'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233'], 'a': 1}

    4.fromkeys(快速生成字典)

    print(dict.fromkeys(['a',2,3,4,5],'nick'))
    

    5.setdefault

    info_dict = {'name': 'wangdapao', 'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233']}
    
    info_dict.setdefault('gender', 123123)  # 有则不变,无则往里面追加了值
    print(info_dict)
    

    {'name': 'wangdapao', 'age': 18, 'height': 120, 'gender': 'female', 'hobby_list': ['dapao', '666', '233']}

    4.存在一个or多个值

    多个值

    5.有序or无序

    无序

    6.可变or不可变

    可变

    dic = {'a':1}
    print(id(dic))
    dic['b'] = 1
    print(id(dic))
    
    2575668575472
    2575668575472
    
    
  • 相关阅读:
    apicloud入门学习笔记1:简单介绍
    QT入门学习笔记1:为什么要选QT及QT软件下载
    Altium Designer入门学习笔记1.软件安装与资料收集
    win 下安装mysql 服务
    几秒后跳转到新页面
    计算3天的日期
    alibaba json
    WebService和Rest Service
    count(*),count(1)和count(主键)的区别
    mybatis替换符号
  • 原文地址:https://www.cnblogs.com/aden668/p/11303878.html
Copyright © 2011-2022 走看看