Python-Basis-7th

周二,晴,记录生活分享点滴

参考博客1:https://www.cnblogs.com/yuanchenqi/articles/5782764.html

参考博客2:https://www.cnblogs.com/alex3714/articles/5717620.html

Python版本:3.5

 

深浅拷贝

浅copy

只copy第一层

# 两个人使用同一银行卡  # 浅copy

father = ["BA", 10001, [5000, 1000]]  # 第一个账户 ,额度5000,余额1000
mother = father.copy()  # 第二个账户

mother[0] = "MA"  # 改变账户名
mother[1] = 10002  # 改变账户代号

father[2][1] -= 200  #第一个账户支出200

print(mother)  #第二个账户同步第一个账户的数据,即显示支出200后的结果

# ['MA', 10002, [5000, 800]]

深copy

完全克隆一份

# 三个人使用同一张银行卡  #深copy
import copy
father = ["BA",10001,[5000,1000]]

mother = father.copy()
mother[0] = "MA"
mother[1] = 10002

child = copy.deepcopy(father)  # 深拷贝 # 将father的钱完全拷贝一份,不走mother的流程
child[0] = "NI"
child[1] = 10003

child[2][1] -= 500 # child支出500
father[2][1] -= 400 # father支出400

print(mother) # ['MA', 10002, [5000, 600]]
# mother只能看到father支出的400,无法发现child支出的500,说明child进行了深拷贝
print(child) # ['NI', 10003, [5000, 500]]

小结:

浅copy无论在字典还是列表中,都只copy第一层

深copy属于完全复制一层

在实际应用中,浅copy不常用

 

集合 set

认识集合

定义:

集合是一个无序的,不重复的数据组合。

li=[1,2,'a','b']
s =set(li)
print(s)    # {1, 2, 'a', 'b'}
 
li2=[1,2,1,'a','a']
s=set(li2)
print(s)  #{1, 2, 'a'}

作用:

去重,把一个列表变成集合,就自动去重了

关系测试,测试两组数据之前的交集、差集、并集等关系

对象:

集合对象是一组无序排列的可哈希的值:集合成员可以做字典的键

#错误示范。表示集合对象不可以是列表格式,列表是可改变的,不是固定的值,是不可哈希的

li=[[1,2],'a','b']
s =set(li) #TypeError: unhashable type: 'list'
print(s)

分类:

可变集合(set):可添加和删除元素,非可哈希的,不能用作字典的键,也不能做其他集合的元素

不可变集合(frozenset):与上面恰恰相反

#错误示范。可变集合不能作为字典的键

li=[1,'a','b']
s =set(li)
dic={s:'123'} #TypeError: unhashable type: 'set'

创建集合

通过set()和frozenset()创建

s1 = set('alvin')  # 可变集合
 
s2= frozenset('yuan')  # 不可变集合
 
print(s1,type(s1))  # {'l', 'v', 'i', 'a', 'n'} <class 'set'>
print(s2,type(s2))  # frozenset({'n', 'y', 'a', 'u'}) <class 'frozenset'>

访问集合

由于集合本身是无序的,所以不能为集合创建索引或切片操作,只能循环遍历或使用in、not in来访问或判断集合元素。

循环遍历的方式有:for循环 、迭代器

s1 = set('alvin')
print('a' in s1)  # True
print('b' in s1)  # False

#s1[1]  #TypeError: 'set' object does not support indexing
 
for i in s1:
    print(i)
# v
# n
# l
# i
# a

更新集合

通过内建方法更新:

s.add()、s.update()、s.remove()、del:删除集合本身

只有可变集合才能更新

# s1 = frozenset('alvin')  # 因为是不可变集合,所以报错
# s1.add(0)  #AttributeError: 'frozenset' object has no attribute 'add'
 
s2=set('alvin')
s2.add('mm')  # 增加一个整体,将mm作为一个整体添加
print(s2)  #{'mm', 'l', 'n', 'a', 'i', 'v'}
 
s2.update('HO') # 添加多个元素,将HO拆解成两个元素
print(s2)  #{'mm', 'l', 'n', 'a', 'i', 'H', 'O', 'v'}
 
s2.remove('l')
print(s2)  #{'mm', 'n', 'a', 'i', 'H', 'O', 'v'}

常用操作

s = set([3,5,9,10])      #创建一个数值集合  
  
t = set("Hello")         #创建一个唯一字符的集合  


a = t | s          # t 和 s的并集  
  
b = t & s          # t 和 s的交集  
  
c = t – s          # 求差集(项在t中,但不在s中)  
  
d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中)  
  
   
  
基本操作:  
  
t.add('x')            # 添加一项  
  
s.update([10,37,42])  # 在s中添加多项  
  
   
  
使用remove()可以删除一项:  
  
t.remove('H')  
  
  
len(s)  
set 的长度  
  
x in s  
测试 x 是否是 s 的成员  
  
x not in s  
测试 x 是否不是 s 的成员  
  
s.issubset(t)  
s <= t  
测试是否 s 中的每一个元素都在 t 中  
  
s.issuperset(t)  
s >= t  
测试是否 t 中的每一个元素都在 s 中  
  
s.union(t)  
s | t  
返回一个新的 set 包含 s 和 t 中的每一个元素  
  
s.intersection(t)  
s & t  
返回一个新的 set 包含 s 和 t 中的公共元素  
  
s.difference(t)  
s - t  
返回一个新的 set 包含 s 中有但是 t 中没有的元素  
  
s.symmetric_difference(t)  
s ^ t  
返回一个新的 set 包含 s 和 t 中不重复的元素  
  
s.copy()  
返回 set “s”的一个浅复制
完整版
a = set([1, 2, 3, 4, 5])
b = set([4, 5, 6, 7, 8])

# in ,not in

# 集合等价与不等价(==, !=)

# intersection()  交集
print(a.intersection(b))  # {4, 5}
print(a & b)

# union()  并集
print(a.union(b))  # {1, 2, 3, 4, 5, 6, 7, 8}
print(a | b)

# difference()  差集
print(a.difference(b))  # {1, 2, 3}  # in a but not in b
print(b.difference(a))  # {8, 6, 7}  # in b but not in a
print(a - b)
print(b - a)

# symmetric_difference()  对称差集,即反向交集
print(a.symmetric_difference(b))  # {1, 2, 3, 6, 7, 8}
print(a ^ b)

# issuperset()  父集
print(a.issuperset(b))  # False
print(a > b)

# issubset()  子集
print(a.issubset(b))  # False
print(a < b)

  

posted @ 2020-05-26 23:21  ChungZhao  阅读(177)  评论(0编辑  收藏  举报