set:无顺序的不重复的集合
list---允许重复,修改
tuple--允许重复,不能修改
set----不允许重复的集合
例子:
s=set()
print s
l1=[11,22,33,22,11]
l2=(11,22,33,44,22)
l3='12345665'
--------------------------输出
set([])
set([33, 11, 22]) set([33, 11, 44, 22]) set(['1', '3', '2', '5', '4', '6'])
-----------------------------------------------------------------------------------------------------------
#clear清空元素
s={11,22,33}
s.clear()
print s------------------------------------------set([])
====================================
#add想集合中添加元素
s.add(44)
print s
--------------------------
set([])
set([44])
====================================
#copy浅拷贝
b=s.copy()
print b
-------------------------
set([44])
====================================
#difference
se={11,22,33}
ms={22,55}
print se.difference(ms) #找se中存在的,ms中不存在的
print ms.difference(se) #找ms中存在的,se中不存在的
-------------------------
set([33, 11])
set([55])
====================================
#difference_update
se={11,22,33}
ms={22,55}
print se.difference_update(ms) #找se中存在的,ms中不存在的,并把se更新
print se,ms
se={11,22,33}
ms={22,55}
print ms.difference_update(se) #找ms中存在的,se中不存在的,并把ms更新
print se,ms
-------------------------
None
set([33, 11]) set([22, 55])
None
set([33, 11, 22]) set([55])
====================================
#discard 移除
se={11,22,33}
se.discard(11)
se.discard(66) #元素不存在不报错
#se.remove(66) #元素不存在报错
print se
-------------------------
set([33, 22])
====================================
#intersection 交集
se={11,22,33}
be={22,95,"随便"}
print se.intersection(be)
-------------------------
set([22])
====================================
#intersection_update 取交集并把自己更新成交集
se={11,22,33}
be={22,95,"随便"}
se.intersection_update(be)
print se
-------------------------
set([22])
====================================
#isdisjoint有交集是false,没有交集数true
se={11,22,33}
be={22,95,"随便"}
print se.isdisjoint(be)
-------------------------
False
====================================
#issubset,issuperset子序列,父序列
se={11,22,33}
be={22,33}
print be.issubset(se)
print se.issuperset(be)
-------------------------
True
True
====================================
#pop移除
se={11,22,33}
se.pop()
print se
-------------------------
set([11, 22])
====================================
#symmetric_difference对称交集
se={11,22,33,44}
be={11,22,55,66}
r1=se.difference(be)
r2=be.difference(se)
print (r1,r2)
ret=se.symmetric_difference(be)
print ret
-------------------------
(set([33, 44]), set([66, 55]))
set([33, 66, 44, 55])
====================================
#symmetric_difference_update对称交集,并更新se
se={11,22,33,44}
be={11,22,55,66}
se.symmetric_difference_update(be)
print se
-------------------------
set([33, 66, 44, 55])
====================================
#union并集
se={11,22,33,44}
be={11,22,55,66}
print se.union(be)
-------------------------
set([33, 66, 11, 44, 22, 55])
====================================
#update更新
se={11,22,33,44}
se.update([444,555,666])
print se
-------------------------
set([33, 555, 11, 44, 22, 666, 444])
====================================
深拷贝,浅拷贝
import copy
n1=123
n2=copy.copy(n1)
print n2,id(n1),id(n2)
n3=copy.deepcopy(n1)
print n2,id(n1),id(n3)
#浅拷贝,在内存中只额外创建第一层数据
#深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
n2=copy.copy(n1)
print n2,id(n1),id(n2)
-----------------------------------------------------------------------------------------------------------