• python_字典操作


    字典        dict  {     }

    字典是无序的,每次输出,里面的元素位置都是会发生不同的变化,字典内需要一个键和值{"键" : "值"},且字典内可以嵌套元组和列表

    字典内,键不得为列表,可通过键来取值,可通过Del删除列表内的键值 

    .keys()    可以取出键

    v = {
        "k1" : "k2",
        "k3" : "k4",
        "k5" : [
            123,
            456,
            (789,10)
        ]
    }
    for i in v.keys():
        print(i)
    #k1
    #k3
    #k5

    .value    获取字典内的值

    v = {
        "k1" : "k2",
        "k3" : "k4",
        "k5" : [
            123,
            456,
            (789,10)
        ]
    }
    for i in v.values():
        print(i)
    #k2
    #k4
    #[123, 456, (789, 10)]

    .items    获取字典中的键值

    v = {
        "k1" : "k2",
        "k3" : "k4",
        "k5" : [
            123,
            456,
            (789,10)
        ]
    }
    for i in v.items():
        print(i)
    #('k1', 'k2')
    ('k3', 'k4')
    ('k5', [123, 456, (789, 10)])

    dict.fromkeys()    生成一个字典,如果参数内的值不是一个,将值作为一个整体,给键配对

    v = dict.fromkeys(["abc","def","hahah"],"hahah")
    print(v)
    #{'abc': 'hahah', 'def': 'hahah', 'hahah': 'hahah'}
    #如果键可以为多个,值必须为一个,否则将把值当成一个整体

    .get()    通过键获取值,如果没有返回None,或者给定的默认参数

    v = {
        "k1" : "v1"
    }
    a = v.get("k11111",11)
    print(a)
    #11(因为不存在K11111这个键所以将默认值11传进去)

    .pop()    将值移除,可以存在到一个位置中

    v = {
        "k1" : "v1",
        "k2" : "v2"
    }
    a = v.pop("k2",11)
    print(v,a)
    #{'k1': 'v1'} v2

    .setdefault()    设置一个键,如果他存在获取这个值

    v = {
        "k1" : "v1",
        "k2" : "v2"
    }
    a = v.setdefault("k11","k3")
    print(v,a)
    #{'k1': 'v1', 'k2': 'v2', 'k11': 'k3'} k3

    .updata()    更新数据,如果没有存在将创建

    v = {
        "k1" : "v1",
        "k2" : "v2"
    }
    v.update(k1 = 123,k2 = 345,k5 = "daw")
    print(v)
    #{'k1': 123, 'k2': 345, 'k5': 'daw'}

      

  • 相关阅读:
    Hierarchy Query (Connect by) and ORA600 ([kkqcbydrv:1])
    The Steps to Create a New Oracle Database
    Change Schema Name (II)
    [转]The differences between V$UNDOSTAT and V$ROLLSTAT
    【Oracle Mgmt】Oracle Character Semantics (NLS_LENGTH_SEMANTICS) and etc...
    [Oracle Mgmt]About Oracle Password File
    Show parameter & Table Not exists
    RMAN Recovery Window and Redundancy Concept
    [PLSQL]Are you sure it will be definitely random? (DBMS_RANDOM.SEED)
    IOT, Secondary Index and Mapping Table
  • 原文地址:https://www.cnblogs.com/Alom/p/10801745.html
Copyright © 2020-2023  润新知