1. 程式人生 > >第七章 字典和集合[DDT書本學習 小甲魚]【3】

第七章 字典和集合[DDT書本學習 小甲魚]【3】

rec type pop list 今天 指向 typeerror pro upd

技術分享圖片

4.copy() 復制字典
a={1:"one",2:"two",3:"three"}
b=a.copy()
print(id(a))
print(id(b))
a[1]="four"
print(a)
print(b)
-------------------
2431102322728
2431102322800
{1: ‘four‘, 2: ‘two‘, 3: ‘three‘}
{1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
====================================
5.pop()和popitem() 給定鍵[彈]出值,後一個[彈]出項
a={1:"one",2:"two",3:"three",4:"four"}
print(a.pop(2))
print(a)
print(a.popitem())
print(a)
-------------------
two
{1: ‘one‘, 3: ‘three‘, 4: ‘four‘}
(4, ‘four‘)
{1: ‘one‘, 3: ‘three‘}
======================================
6. setdefault()方法和get()方法相似,區別是get()找不到返回空或指定值
而setdefault()方法在找不到的時候,進行設置添加值。
a={1:"one",2:"two",3:"three",4:"four"}
print(a.setdefault(3))
print(a.setdefault(5))
print(a)
-----------------------
three
None
{1: ‘one‘, 2: ‘two‘, 3: ‘three‘, 4: ‘four‘, 5: None}
==============================================================
7. update() 更新字典 修改而不增加
pets={1:"龍",2:"虎",3:"豬",4:"狗",5:"雞",6:"猴"}
pets.update(6="鼠")
print(pets)
------以上代碼出現錯誤,錯誤原因不可知!!---------更正代碼如下
pets={"a":"龍","b":"虎","c":"豬","d":"狗","e":"雞","f":"猴"}
pets.update(f="鼠")
print(pets)
----------------------------
{‘a‘: ‘龍‘, ‘b‘: ‘虎‘, ‘c‘: ‘豬‘, ‘d‘: ‘狗‘, ‘e‘: ‘雞‘, ‘f‘: ‘鼠‘}
=====================================================================
6.2節末尾買下伏筆,函數在收集參數的時候,用到了*,而另外一種是**
def test(*p):
print("有%d個參數"%len(p))
print("第二個參數是:",p[1])
a=[1,2,3,4,5,6,7,8,9]
test(*a) #必須加上*號 進行解包 否則出錯
---------------------------------
有9個參數
第二個參數是: 2
================================================
收集參數,有兩種打包方式:以元組形式打包,以字典形式打包。
def test(**p):
print("有%d個參數"%len(p))
print("它們分別是:",p)
k={a:1,b:2,c:3,d:4}
test(**k)
-------------------------------
有4個參數
它們分別是: {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4}
===============================================

7.2集合:在我的世界裏,你就是【唯一】
上節講述了字典,在Python裏字典對應的是數學裏的映射。而今天學習字典的表親:集合
請看如下代碼:
num1={} #默認空字典
print(type(num1))
num2={1,2,3,4,5} #和列表
print(type(num2))
--------------------
<class ‘dict‘>
<class ‘set‘>
=====================================集合用來體現唯一特性
舉例如下 重復的算一個!!!!!!!
num={1,2,3,4,5,4,3,2,1}
print(num)
------------------------
{1, 2, 3, 4, 5}
============================可以對重復數據進行清理,方便快捷
但是同時要註意,集合時無序的,不能用索引進行引用某一個元素。
num={1,2,3,4,5,4,3,2,1}
print(num[2])
--------- 報錯如下 --------------
Traceback (most recent call last):
File "C:/Users/Daodantou/PycharmProjects/s14/day7/t3.py", line 2, in <module>
print(num[2])
TypeError: ‘set‘ object does not support indexing
======================================================================
7.2.1 創建集合 有2種方法 用{}或者用set()方法【字典形式,列表形式】
代碼如下
set1={"飛鳥","魷魚","豹子","老虎"}
set2=set(["飛鳥","魷魚","豹子","老虎"])
print(set1==set2)
---------------------------------------
True
===========================================
以前要求去除列表[1,2,3,4,5,6,3,1,0]種的重復元素,需要這麽寫:
list1=[1,2,3,4,5,5,3,1,0]
list2=list1[:] #切片操作! 如果list2=list1 則指向同一個列表
list1.clear()
for each in list2:
if each not in list1:
list1.append(each)
print(list1)
-------------------------
[1, 2, 3, 4, 5, 0]
=========================================學習了集合之後,可以這麽幹=====
list1=[1,2,3,4,5,5,3,1,0]
list1=list(set(list1)) #set()創建的集合 導致了無序 要特別註意
print(list1)
-------------------------
[0, 1, 2, 3, 4, 5]
==============================================================
7.2.2 訪問集合
由於集合中的元素是無序的,所以無法用下標來進行訪問,可以用叠代方法找元素。
set1={1,2,3,4,5,4,3,2,1,0}
for each in set1:
print(each,end=" ")
-----------------------------在集合建立之初,就自動去除重復了
0 1 2 3 4 5
====================也可以用in和not in判斷元素是否存在集合中
set1={1,2,3,4,5,4,3,2,1,0}
print(0 in set1)
print("k" in set1)
print(7 not in set1)
----------------------------------
True
False
True
=======================================
使用add()方法可以為集合添加元素
使用remove()方法可以刪除集合中的已知元素
set1={1,2,3,4,5,4,3,2,1,0}
set1.add("wo")
print(set1)
set1.remove(0)
print(set1)
---------------------
{0, 1, 2, 3, 4, 5, ‘wo‘}
{1, 2, 3, 4, 5, ‘wo‘}
=================================================================
7.2.3 不可變集合
有時候,我們希望集合中的數據有穩定性,也就是像元組一樣不能隨意增加或刪除元素
那麽我們可以定義不可變集合,使用frozenset()函數,沒錯就是冰凍【frozen】起來:
set1=frozenset({1,2,3,4,5})
set1.add("k")
print(set1)
------------------報錯!!!!!!!!!!
Traceback (most recent call last):
File "C:/Users/Daodantou/PycharmProjects/s14/day7/t3.py", line 2, in <module>
set1.add("k")
AttributeError: ‘frozenset‘ object has no attribute ‘add‘

第七章 字典和集合[DDT書本學習 小甲魚]【3】