zoukankan      html  css  js  c++  java
  • python-第01章06章节-python字典的使用

    1.字典的操作

            字典一种key - value 的数据类型,使用就像我们上学的字典,通过笔画,字典来查对应页的详细内容。

    语法:

    info = {
        'student1101' : "hanjiali",
        'student1102' : "hanjiali1",
        'student1103' : "hanjiali2",
    }
    
    info = {
    'student1101' : "hanjiali",
    'student1102' : "hanjiali1",
    'student1103' : "hanjiali2",
    }
    print(info)
    print("-----------------------------------")
    #查(存在的情况下不出错,不存在报错)
    print(info["student1101"])
    print("-----------------------------------")
    #查(标准方法)
    print(info.get("student1101"))
    print("-----------------------------------")
    #试一个人在不在
    print("student1104" in info )
    #输出T or F
    print("-----------------------------------")

    print("-----------------------------------")
    #
    info["student1101"] = "韩佳丽"
    print(info)
    print("-----------------------------------")
    #1(标准删除)
    info.pop('student1103')
    print(info)
    print("-----------------------------------")
    #2
    del info["student1102"]
    print(info)
    print("-----------------------------------")

    运行结果:

    {'student1101': 'hanjiali', 'student1102': 'hanjiali1', 'student1103': 'hanjiali2'}
    -----------------------------------
    hanjiali
    -----------------------------------
    hanjiali
    -----------------------------------
    False
    -----------------------------------
    {'student1101': '韩佳丽', 'student1102': 'hanjiali1', 'student1103': 'hanjiali2'}
    -----------------------------------
    {'student1101': '韩佳丽', 'student1102': 'hanjiali1'}
    -----------------------------------
    {'student1101': '韩佳丽'}
    -----------------------------------
    

     2.字典的嵌套

    ar_cata = {
    "homework" : {
    'stud_1101': ["认真完成"],
    'stud_1102': ["很差"],
    'stud_1103': ["尽最大努力"],




    },

    "class" : {
    's_1101': ["上课注意力集中"],
    's_1102': ["不听课"],
    's_1103': ["还可以"],

    },

    "student" : {

    '1101' : "hanjiali",
    '1102' : "hanjiali1",
    '1103' : "hanjiali2",

    }


    }
    ar_cata["student"]["1103"] = "马瑞龙"
    print(ar_cata)
    print("-----------------------------------")
    #创建新的

    ar_cata.setdefault("地区",["内蒙古"])
    print(ar_cata)

     运行结果:(尽量不写中文,怕字符转换出错)

    {'homework': {'stud_1101': ['认真完成'], 'stud_1102': ['很差'], 'stud_1103': ['尽最大努力']}, 'class': {'s_1101': ['上课注意力集中'], 's_1102': ['不听课'], 's_1103': ['还可以']}, 'student': {'1101': 'hanjiali', '1102': 'hanjiali1', '1103': '马瑞龙'}}
    -----------------------------------
    {'homework': {'stud_1101': ['认真完成'], 'stud_1102': ['很差'], 'stud_1103': ['尽最大努力']}, 'class': {'s_1101': ['上课注意力集中'], 's_1102': ['不听课'], 's_1103': ['还可以']}, 'student': {'1101': 'hanjiali', '1102': 'hanjiali1', '1103': '马瑞龙'}, '地区': ['内蒙古']}

     3.字典的合并

    info = {
        'student1101' : "hanjiali",
        'student1102' : "hanjiali1",
        'student1103' : "hanjiali2",
    }
    
    b = {
    
        'student1101': "hanjiali",
        1 : 3,
        2 : 2
    
    }
    
    info.update(b)
    print(info)
    #转化成列表
    print("---------------------------------------------------------------")
    print(info.items())

     结果:

    {'student1101': 'hanjiali', 'student1102': 'hanjiali1', 'student1103': 'hanjiali2', 1: 3, 2: 2}
    ---------------------------------------------------------------
    dict_items([('student1101', 'hanjiali'), ('student1102', 'hanjiali1'), ('student1103', 'hanjiali2'), (1, 3), (2, 2)])

     4.字典的循环

    info = {
        'student1101' : "hanjiali",
        'student1102' : "hanjiali1",
        'student1103' : "hanjiali2",
    }
    
    b = {
    
        'student1101': "hanjiali",
        1 : 3,
        2 : 2
    
    }
    
    info.update(b)
    print(info)
    print("---------------------------------------------------------------")
    print(info.items())
    
    for i in info :
        print(i,info[i])

    输出结果:

    student1101 hanjiali
    student1102 hanjiali1
    student1103 hanjiali2
    1 3
    2 2
    

     

  • 相关阅读:
    159
    158
    157
    156
    155
    高中生都能看懂的莫比乌斯反演
    洛谷 P4449 于神之怒加强版
    洛谷 P3455 [POI2007]ZAP-Queries
    洛谷 P1829 [国家集训队]Crash的数字表格 / JZPTAB
    Dirichlet卷积
  • 原文地址:https://www.cnblogs.com/hanjiali/p/11325816.html
Copyright © 2011-2022 走看看