• 第七章 字典和集合[DDT书本学习 小甲鱼]【2】


    7.1.2 字典的各种内置方法
    在序列里为不存在位置赋值,会出现错误;
    而在字典不存在得位置赋值,会创建。工厂函数(类型)
    以前学过 str(),int(),list(),tuple().......

    1.fromkeys() 用于创建和返回一个新的字典 不是修改
    2个参数 第一个是键,第二个可选,默认None
    举例如下
    dict1={}
    dict1.fromkeys((1,2,3))
    print(dict1)
    ------------------------
    {} #????????????????????? 原始字典是空的,方法返回的是一个新字典!!!!
    改写如下
    dict1={}
    print(dict1.fromkeys((1,2,3)))
    ----------------------------
    {1: None, 2: None, 3: None}
    =======================================
    dict2={}
    print(dict2.fromkeys((1,2,3),"Number"))
    ----------------------------
    {1: 'Number', 2: 'Number', 3: 'Number'}
    ==========================================
    dict3={}
    print(dict3.fromkeys((1,2,3),("one","two","three")))
    -----------------------------------
    {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
    ============================================================
    dict3字典改新过程后,并不是对应关系,而是将后面的所有当作一个值。

    2.keys()和values()和items()
    dict1={}
    dict1=dict1.fromkeys(range(32),"赞")
    print(dict1)
    -----------------------------
    {0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞',......31 '赞'}
    =======================================
    dict1={}
    dict1=dict1.fromkeys(range(32),"赞")
    for eachkey in dict1.keys():
    print(eachkey)
    -------------
    0
    1
    2
    .
    .
    31
    =====================================
    dict1={}
    dict1=dict1.fromkeys(range(32),"赞")
    for eachvalue in dict1.values():
    print(eachvalue)
    --------------

    .
    .
    .

    =========================================
    dict1={}
    dict1=dict1.fromkeys(range(32),"赞")
    for eachItem in dict1.items():
    print(eachItem)
    ---------------------------
    (0, '赞')
    (1, '赞')
    .
    .
    (31, '赞')
    ===================================================
    当我们不明确字典内部的键和项的时候,引用会出错。如下
    dict1={}
    dict1=dict1.fromkeys(range(32),"赞")
    print(dict1[32])
    -------------------------------
    KeyError: 32 #提示没有这个键
    ===================================================
    如何解决以上问题呢?
    3.get() 括号内填写的是键 不是序列号
    dict2={1:"one",2:"two",3:"three"}
    print(dict2.get(3))
    print(dict2.get(0))
    -----------------------
    three
    None
    ===========================================
    如果想找不到该参数时候,返回指定值 比如“木有”:
    print(dict2.get(32,"木有"))
    ==============================================
    如果不知道一个键 是否再字典中 可以用成员资格操作符 in或not in
    dict2={1:"one",2:"two",3:"three"}
    print(3 in dict2)
    print(4 in dict2)
    print(5 not in dict2)
    ------------------------
    True
    False
    True
    ==================================================
    再字典中检查键的成员资格比序列更高效,当数据规模大的时候,差距很明显。
    原因:字典采用哈希方法一对一找到成员,序列采用的是迭代的方式阻隔比对。
    清空字典,则使用 clear()方法
    dict2={1:"one",2:"two",3:"three"}
    dict2.clear()
    print(dict2)
    -------------------
    {}
    ===============================================
    使用变量名赋值为一个空字典方法来清空
    dict2={1:"one",2:"two",3:"three"}
    dict2={}
    print(dict2)
    -----------------------
    {}
    ==================效果看似一样啊?思考如下代码 ==========================
    a={"name":"Daodantou"}
    b=a
    print(b)
    a={}
    print(a)
    print(b)
    -------------
    {'name': 'Daodantou'}
    {}
    {'name': 'Daodantou'}
    =========================
    a={"name":"Daodantou"}
    b=a
    print(b)
    a.clear()
    print(a)
    print(b)
    ---------------------------
    {'name': 'Daodantou'}
    {}
    {}
    ===============================
    数据密码时候,采用赋值为空字典存在安全隐患,而clear()方法安全些。
    拓展思考 在这个问题上 有关 整型 字符串区别
    a=1
    b=a
    print(b)
    a=7
    print(a)
    print(b)
    ---------
    1
    7
    1
    -------=================-----
    a="坦克"
    b=a
    print(b)
    a="飞机"
    print(a)
    print(b)
    --------------------
    坦克
    飞机
    坦克
    =================================

    Daodantou:“不积跬步,无以至千里.”
  • 相关阅读:
    使用ueditor实现多图片上传案例——DaoImpl层(BaseDaoUtilImpl)
    使用ueditor实现多图片上传案例——Dao层(IShoppingDao)
    使用ueditor实现多图片上传案例——Dao层(IBaseDaoUtil)
    使用ueditor实现多图片上传案例——Dao层(BaseDao)
    使用ueditor实现多图片上传案例——实体类(Shopping.java)
    使用ueditor实现多图片上传案例
    java实现人脸识别源码【含测试效果图】——前台页面层(login.jsp)
    oracle 使用leading, use_nl, rownum调优(引用)
    oracle 使用leading, use_nl, rownum调优(引用)
    [转载]spring+proxool
  • 原文地址:https://www.cnblogs.com/daodantou/p/10337770.html
Copyright © 2020-2023  润新知