• python3 字典(dict)基础


    1、定义及初始化

    1.1、直接初始化

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info)

      打印输出结果:

    {'stu1101': 'jack', 'stu1102': 'tom', 'stu1103': 'mary'}

    1.2、先定义后赋值

    records ={}
    records["stu1101"] = 90
    records["stu1102"] = 80
    records["stu1103"] = 70
    print(records)

      打印输出结果:

    {'stu1101': 90, 'stu1102': 80, 'stu1103': 70}

    2、方法

    2.1、setdefault()

      设置默认值。

    2.2.1、对新的key设置默认值

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    info.setdefault("stu1100", "robin")
    print(info)

      打印输出结果:

    {'stu1101': 'jack', 'stu1102': 'tom', 'stu1103': 'mary', 'stu1100': 'robin'}

    2.2.2、对已存在的key设置默认值,原来的值不改变

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    info.setdefault("stu1101", "robin")
    print(info)

      打印输出结果:

    {'stu1101': 'jack', 'stu1102': 'tom', 'stu1103': 'mary'}

    2.3、keys()与values()

    2.3.1、keys()

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info.keys())
    print(type(info.keys()))

      打印输出结果:

    dict_keys(['stu1101', 'stu1102', 'stu1103'])
    <class 'dict_keys'>

    2.3.2、读取keys()的元素

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    info_keys_list = list(info.keys())
    print(info_keys_list[0])
    print(info_keys_list[1])
    print(info_keys_list[2])

      打印输出结果:

    stu1101
    stu1102
    stu1103
    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    for key in info.keys():
        print(key)

      打印输出结果:

    stu1101
    stu1102
    stu1103

    2.3.3、values()

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info.values())
    print(type(info.values()))

      打印输出结果:

    dict_values(['jack', 'tom', 'mary'])
    <class 'dict_values'>

    2.4、items()

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info.items())
    for k,v in info.items():
        print("%s: %s"%(k,v))

      打印输出结果:

    dict_items([('stu1101', 'jack'), ('stu1102', 'tom'), ('stu1103', 'mary')])
    stu1101: jack
    stu1102: tom
    stu1103: mary

    2.5、get()

      获取key对应的键值。

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info.get('stu1103'))
    print(info.get('stu1111',None))

      打印输出结果:

    mary
    None

    2.6、字典中是否有某个key,info默认取keys()

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print("stu2200" in info)
    print("stu1101" in info)
    # 等同于上面的写法
    print("stu1101" in info.keys())

      打印输出结果:

    False
    True
    True

    2.7、字典中是否有某个value

    # info默认去keys,因此这用用法是错误的
    print("tom" in info)
    # 直接去info的values(),再判断"tom"是否在其中
    print("tom" in info.values())

      打印输出结果:

    False
    True

    2.8、update()

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    b = {
        "stu1101": "alex",
        "stu1104": "white",
        "stu1105": "black",
    }
    info.update(b)
    print(info)

      打印输出结果:

    {'stu1101': 'alex', 'stu1102': 'tom', 'stu1103': 'mary', 'stu1104': 'white', 'stu1105': 'black'}

    2.9、del

    2.9.1、删除字典中的键

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    del info["stu1101"]
    print(info)

      打印输出结果:

    {'stu1102': 'tom', 'stu1103': 'mary'}

    2.9.2、删除字典

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    del info
    print(info)

      打印输出结果:

    name 'info' is not defined

    2.10、pop()

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info.pop("stu1101"))

      打印输出结果:

    jack

    2.11、popitem()

       随机返回并删除字典中的一对键和值(一般删除末尾对)。

    info = {
        "stu1101": "jack",
        "stu1102": "tom",
        "stu1103": "mary",
    }
    print(info.popitem())
    print(info)

      打印输出结果:

    ('stu1103', 'mary')
    {'stu1101': 'jack', 'stu1102': 'tom'}

    2.12、fromkeys()

      创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。

    key_list = ["name","age","height"]
    dic = dict.fromkeys(key_list,None)
    print(dic)

      打印输出结果:

    'name': None, 'age': None, 'height': None}

    2.13、sorted()

      所有可迭代的对象进行排序操作  

      sort 与 sorted 区别:

        sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

        list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

    a = {6:2,8:0,1:4,-5:6,99:11,4:22}
    print(  sorted(a.items()) )
    print(  sorted(a.items(),key=lambda x:x[1]) )
    print(  sorted(a.items(),key=lambda x:x[0]) )

      打印输出结果:

    [(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
    [(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]
    [(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]

    2.14、zip()

      用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

    a = [1,2,3,4,5,6]
    b = ['a','b','c','d']
    
    for i in zip(a,b):
        print(type(i))
        print(i)
    print("*"*50)
    print(list(zip(a,b)))

      打印输出结果:

    <class 'tuple'>
    (1, 'a')
    <class 'tuple'>
    (2, 'b')
    <class 'tuple'>
    (3, 'c')
    <class 'tuple'>
    (4, 'd')
    **************************************************
    [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
  • 相关阅读:
    38) 收集centos所有版本镜像下载地址
    37) 查看linux 操作系统为多少位
    php面向对象高级应用一
    php form表单的验证+提交到数据库
    php获取form表单数据
    php form表单的提交
    php form表单概念
    php日期和时间的应用
    php日期和时间函数
    php字符串函数操作实例(2)
  • 原文地址:https://www.cnblogs.com/bad-robot/p/9678919.html
Copyright © 2020-2023  润新知