集合
s2={‘python’,2,3,5}
s2
{‘python’, 2, 3, 5}
s2.add(‘book’)
s2
{2, 3, 5, ‘book’, ‘python’}
s2.pop()
2
s2
{3, 5, ‘book’, ‘python’}
s2.remove(3)
s2
{5, ‘book’, ‘python’}
s2.discard(3)
s2
{5, ‘book’, ‘python’}
s2.discard(‘book’)
s2
{5, ‘python’}
拷贝
b1=[‘name’,123,[‘book’,‘python’]]
b1
[‘name’, 123, [‘book’, ‘python’]]
b2=b1.copy()
b2
[‘name’, 123, [‘book’, ‘python’]]
b1 is b2(浅拷贝)
False
b1[1]=55
b1
[‘name’, 55, [‘book’, ‘python’]]
b2
[‘name’, 123, [‘book’, ‘python’]]
b1[2][1]
‘python’
b1[2][1]=99
b1
[‘name’, 55, [‘book’, 99]]
b2
[‘name’, 123, [‘book’, 99]]
深拷贝 完全独立
import copy
b1
[‘name’, 55, [‘book’, 99]]
b3=copy.deepcopy(b1)
b1[2][1]=‘java’
b1
[‘name’, 55, [‘book’, ‘java’]]
b3
[‘name’, 55, [‘book’, 99]]
集合的运算
a={2,3,5,8,4,5}
b={2,5,4,3}
a|b 合并集
{2, 3, 4, 5, 8}
a.union(b)
{2, 3, 4, 5, 8}
a&b 交集
{2, 3, 4, 5}
a.intersection(b)
{2, 3, 4, 5}
a-b 差集
{8}
a.difference(b)
{8}