標籤:ror 深拷貝 ber 練習 三元運算 intersect empty nts 變數
三元運算
三元運算(三目運算),是對簡單的條件陳述式的縮寫
# 書寫格式 result = 值1 if 條件 else 值2 # 如果條件成立,那麼將 “值1” 賦值給result變數,否則,將“值2”賦值給result變數
基礎資料型別 (Elementary Data Type)補充
set
set集合,是一個無序且不重複的元素集合
class set(object): """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): # real signature unknown """ Add an element to a set,添加元素 This has no effect if the element is already present. """ pass def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. 清除內容""" pass def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. 淺拷貝 """ pass def difference(self, *args, **kwargs): # real signature unknown """ Return the difference of two or more sets as a new set. A中存在,B中不存在 (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. 從當前集合中刪除和B中相同的元素""" pass def discard(self, *args, **kwargs): # real signature unknown """ Remove an element from a set if it is a member. If the element is not a member, do nothing. 移除指定元素,不存在不保錯 """ pass def intersection(self, *args, **kwargs): # real signature unknown """ Return the intersection of two sets as a new set. 交集 (i.e. all elements that are in both sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. 取交集並更更新到A中 """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. 如果沒有交集,返回True,否則返回False""" pass def issubset(self, *args, **kwargs): # real signature unknown """ Report whether another set contains this set. 是否是子序列""" pass def issuperset(self, *args, **kwargs): # real signature unknown """ Report whether this set contains another set. 是否是父序列""" pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. 移除元素 """ pass def remove(self, *args, **kwargs): # real signature unknown """ Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. 移除指定元素,不存在保錯 """ pass def symmetric_difference(self, *args, **kwargs): # real signature unknown """ Return the symmetric difference of two sets as a new set. 對稱差集 (i.e. all elements that are in exactly one of the sets.) """ pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. 對稱差集,並更新到a中 """ pass def union(self, *args, **kwargs): # real signature unknown """ Return the union of sets as a new set. 並集 (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. 更新 """ pass
練習:尋找差異
#!/usr/bin/env python# -*-coding:utf-8 -*-old_dict = { "#1":11, "#2":22, "#3":100,}new_dict = { "#1":33, "#4":22, "#7":100,}old_set = set(old_dict.keys())new_set = set(new_dict.keys())same_set = old_set.intersection(new_set)up_set = new_set.difference(old_set)ret_dict = {}for k in same_set: ret_dict[k]=new_dict[k]for i in up_set: ret_dict[i]=new_dict[i]print(ret_dict)# print(old_set)# print(new_set)深淺拷貝
一、數字和字串
對於 數字 和 字串 而言,賦值、淺拷貝和深拷貝無意義,因為其永遠指向同一個記憶體位址。
import copya = 132b = ‘Ales‘# ## 淺拷貝 ##aa = copy.copy(a)bb = copy.copy(b)# ## 深拷貝 ##aaa = copy.deepcopy(a)bbb = copy.deepcopy(b)print(id(a),id(aa),id(aaa))print(id(b),id(bb),id(bbb))# ## 1691783456 1691783456 1691783456# ## 47227376 47227376 47227376
二、其他基礎資料型別 (Elementary Data Type)
對於字典、元祖、列表 而言,進行賦值、淺拷貝和深拷貝時,其記憶體位址的變化是不同的。
1、賦值
賦值,只是建立一個變數,該變數指向原來記憶體位址,如:
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n2 = n1
2、淺拷貝
淺拷貝,在記憶體中只額外建立第一層資料
import copy n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n3 = copy.copy(n1)print(id(n1),id(n3))print(id(n1["k1"]),id(n3["k1"]))# 44947080 45383304# 45327056 45327056
3、深拷貝
深拷貝,在記憶體中將所有的資料重新建立一份(排除最後一層,即:python內部對字串和數位最佳化)
import copyn1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}n4 = copy.deepcopy(n1)print(id(n1),id(n4))print(id(n1["k3"]),id(n4["k3"]))print(id(n1["k3"][0]),id(n4["k3"][0]))# 56088200 56460680# 58962760 58930696# 56506216 56506216
函數
【Python之路】第四篇--Python基礎之函數