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)注
55文章
4768瀏覽量
84376 -
ASSERT
+關(guān)注
關(guān)注
0文章
17瀏覽量
7221
發(fā)布評論請先 登錄
相關(guān)推薦
評論