1.基本的增刪改查, 再加上discard和pop
def test_1():
# 增刪改查,discard,
set1 = {1, 2}
set1.add(3)
assert {1, 2, 3} == set1, 'add error'
set1.remove(2)
assert {1, 3} == set1, 'remove error'
set1.update({1, 4})
assert {1, 3, 4} == set1, 'update 1 error'
set1.update((5, ))
assert {1, 3, 4, 5} == set1, 'update 2 error'
e = 5 if 5 in set1 else None
assert e == 5, 'in error'
# 區(qū)別就是remove的元素在set當(dāng)中沒有的話會報錯,而discard不會
set1.discard(5)
assert {1, 3, 4} == set1, 'discart error'
res = set1.pop()
assert 1== res and {3,4} == set1, 'pop error'
2.difference[差],union[并],intersection[交],symmetric_difference[補]
def test_2():
# - | & ^, difference,union,intersection,symmetric_difference
set1 = {1, 2, 3}
set2 = {2, 3, 4}
setx = set1 - set2
assert {1} == setx, '- error'
setx = set1 | set2
assert {1, 2, 3, 4} == setx, '| error'
setx = set1 & set2
assert {2, 3} == setx, '& error'
setx = set1 ^ set2
assert {1, 4} == setx, '^ error'
setx = set1.difference(set2)
assert {1} == setx, '- error'
setx = set1.union(set2)
assert {1, 2, 3, 4} == setx, '| error'
setx = set1.intersection(set2)
assert {2, 3} == setx, '& error'
setx = set1.symmetric_difference(set2)
assert {1, 4} == setx, '^ error'
pass
3.set的構(gòu)造方法, in,enumerate
def test_3():
# 方法:set(p), in,enumerate
set1 = set()
assert set() == set1, 'set() error'
set1 = set([1, 2])
assert {1, 2} == set1, '[1,2] error'
set1 = set((1, ))
assert {1} == set1, r'{1} error'
set1 = set('abc')
assert {'a', 'b', 'c'} == set1, 'abc error'
set_indexs = [i for i, v in enumerate(set1)]
assert [0, 1, 2] == set_indexs, 'enumerate error'
pass
4.sorted
def test_4():
# sorted
set1 = {'b', 'a', 'c'}
sorted(set1)
assert {'a', 'b', 'c'} == set1, 'sorted error'
pass
5.淺復(fù)制與深復(fù)制
import copy
class Person:
def __init__(self, a: int, b: int):
self.a = a
self.b = b
pass
def test_5():
# 淺復(fù)制 和 深度復(fù)制
set1 = {1, Person(2, 3)}
set2 = {i for i in set1}
set2.update({4})
assert not {4}.issubset(set1), 'copy 1 error'
assert {4}.issubset(set2), 'copy 2 error'
set2 = set1.copy()
for i in set2:
if type(i) == Person:
i.a = 4
p1: Person = [i for i in set1 if type(i) == Person][0]
p2: Person = [i for i in set2 if type(i) == Person][0]
assert 4 == p1.a, 'copy 3 error'
assert 4 == p2.a, 'copy 4 error'
set1 = {1, Person(2, 3)}
set2 = copy.deepcopy(set1)
for i in set2:
if type(i) == Person:
i.a = 4
p3: Person = [i for i in set1 if type(i) == Person][0]
p4: Person = [i for i in set2 if type(i) == Person][0]
assert 2 == p3.a, 'copy 3 error'
assert 4 == p4.a, 'copy 4 error'
pass
6.去重之后保證之前的順序
def test_6():
'''
set 去重,并且保證之前的順序
'''
list1 = [1, 2, 7, 2, 5]
list2 = list(set(list1))
assert [1, 2, 5, 7] == list2, 'set sort error'
list2.sort(key=list1.index)
assert [1, 2, 7, 5] == list2, 'set sort 2 error'
審核編輯:劉清
-
python
+關(guān)注
關(guān)注
58文章
4882瀏覽量
90281 -
ASSERT
+關(guān)注
關(guān)注
0文章
17瀏覽量
7675
發(fā)布評論請先 登錄
使用PYTHON進(jìn)行的跨平臺仿真
[VirtualLab] 使用Python運行VirtualLab Fusion光學(xué)仿真
如何在 VisionFive 上使用 Python 包?
1688 商品詳情 API 調(diào)用與數(shù)據(jù)解析 Python 實戰(zhàn)
Termux中調(diào)試圣誕樹Python代碼
termux調(diào)試python猜數(shù)字游戲
termux如何搭建python游戲
python app不能運行怎么解決?
【創(chuàng)龍TL3562-MiniEVM開發(fā)板試用體驗】7、python測試
linux虛擬環(huán)境中調(diào)用Linux 版matlab編譯的python庫時出錯
基礎(chǔ)篇3:掌握Python中的條件語句與循環(huán)
harmony-utils之ArrayUtil,集合工具類
Hi3861 wifiiot_hispark_pegasus 按教程安裝python3 -m pip install build/lite 報錯
如何破解GPU集群集合通信路徑的“黑盒”難題?
python之集合set的基本步驟分享
評論