• Python3基础5——字典dict


    字典 dict 符号{} 大括号  花括号  无序
    1:可以存在空字典a={}
    2:字典里面数据存储的方式: key:value
    3:字典里面value可以包含任何类型的数据
    4:字典里面的元素  根据逗号来进行分隔
    5:字典里面的key必须是唯一的

    1 a={"class":"python11",
    2     "student":119,
    3     "teacher":"girl",
    4     "t_age":20,
    5     "score":[99,88.8,100.5]}
    6  
    7 # 字典取值:字典[key]
    8 print(a["t_age"])
    9 <<< 20

    删除 pop(key) 指明删除的值的key

     1 a={"class":"python11",
     2    "student":119,
     3    "teacher":"girl",
     4    "t_age":20,
     5    "score":[99,88.8,100.5]}
     6 res=a.pop("teacher")  # 删除值"girl"
     7 print(res)
     8 <<< girl
     9  
    10 print(a)
    11 <<< {'student': 119, 
    12         'class': 'python11', 
    13         'score': [99, 88.8, 100.5], 
    14         't_age': 20}

    修改 a[已存在的key]=新value  key是字典中已存在的

    1 a = {
    2 'student': 119, 
    3 'class': 'python11', 
    4 'score': [99, 88.8, 100.5], 
    5 't_age': 20}
    6 # 修改t_age对应的值,要保证t_age是已存在的key
    7 a["t_age"]=18
    8 print(a)
    9 <<< {'student': 119, 'class': 'python11', 'score': [99, 88.8, 100.5], 't_age': 18}

    新增 a[不存在的key] = 新value  key是字典中不存在的

    1 a = {
    2 'student': 119, 
    3 'class': 'python11', 
    4 'score': [99, 88.8, 100.5], 
    5 't_age': 20}
    6 # 新增name对应的值,要保证name是不存在的key
    7 a["name"]="monica"
    8 print(a)
    9 <<< {'student': 119, 'class': 'python11', 'score': [99, 88.8, 100.5], 't_age': 18,'name':"monica"}
  • 相关阅读:
    接口的经典使用方法
    多态的程序例子
    log4j常用配置过程
    log4j.properties对于web app摆放的位置
    MySQL优化实例
    No sql 相关
    yii直接执行sql
    android NDK JNI设置自己的log输出函数
    android web 网址收集
    WebKit加载网页的流程
  • 原文地址:https://www.cnblogs.com/monica711/p/9804503.html
Copyright © 2020-2023  润新知