第七章 字典和集合[DDT书本学习 小甲鱼]【2】
7.1.2 字典的各種內(nèi)置方法
在序列里為不存在位置賦值,會出現(xiàn)錯誤;
而在字典不存在得位置賦值,會創(chuàng)建。工廠函數(shù)(類型)
以前學過 str(),int(),list(),tuple().......
1.fromkeys() 用于創(chuàng)建和返回一個新的字典 不是修改
2個參數(shù) 第一個是鍵,第二個可選,默認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字典改新過程后,并不是對應(yīng)關(guān)系,而是將后面的所有當作一個值。
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, '贊')
===================================================
當我們不明確字典內(nèi)部的鍵和項的時候,引用會出錯。如下
dict1={}
dict1=dict1.fromkeys(range(32),"贊")
print(dict1[32])
-------------------------------
KeyError: 32 #提示沒有這個鍵
===================================================
如何解決以上問題呢?
3.get() 括號內(nèi)填寫的是鍵 不是序列號
dict2={1:"one",2:"two",3:"three"}
print(dict2.get(3))
print(dict2.get(0))
-----------------------
three
None
===========================================
如果想找不到該參數(shù)時候,返回指定值 比如“木有”:
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
==================================================
再字典中檢查鍵的成員資格比序列更高效,當數(shù)據(jù)規(guī)模大的時候,差距很明顯。
原因:字典采用哈希方法一對一找到成員,序列采用的是迭代的方式阻隔比對。
清空字典,則使用 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'}
{}
{}
===============================
數(shù)據(jù)密碼時候,采用賦值為空字典存在安全隱患,而clear()方法安全些。
拓展思考 在這個問題上 有關(guān) 整型 字符串區(qū)別
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)
--------------------
坦克
飛機
坦克
=================================
轉(zhuǎn)載于:https://www.cnblogs.com/daodantou/p/10337770.html
總結(jié)
以上是生活随笔為你收集整理的第七章 字典和集合[DDT书本学习 小甲鱼]【2】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡逾期会冻结其它银行卡吗?看完你还敢
- 下一篇: 信用卡逾期能坐飞机吗?看完你就清楚了!