python之集合set的基本步骤分享
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'
# 区别就是remove的元素在set当中没有的话会报错,而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的构造方法, 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.浅复制与深复制
import copy
class Person:
def __init__(self, a: int, b: int):
self.a = a
self.b = b
pass
def test_5():
# 浅复制 和 深度复制
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'
审核编辑:刘清