python3集合类调用方法

(1)add方法(添加)

s = {1,3,6,8}
s.add(9)
print(s)

返回值:{1, 3, 6, 8, 9}

(2)clear方法(清空)

s = {1,3,6,8}
s.clear()
print(s)

返回值:set()

 (3)copy方法(复制)

s = {1,3,6,8}
s1 = s.copy()
print(s1)

返回值:{8, 1, 3, 6}

(4)pop方法(随机删除) 

s = {'th',1,3,6,8,'a','b','c'}
s.pop()
print(s)

返回值:{3, 'th', 6, 8, 'c', 'b', 'a'}

(5)remove方法(指定删除,元素不存在会报错)

s = {'th',1,3,6,8,'a','b','c'}
s.remove('th')
print(s)

返回值:{1, 3, 'a', 6, 8, 'b', 'c'}

(6)discard方法(指定删除,元素不存在不会报错)

s = {'a','b','c','d','e',1,2,3}
s.discard('a')
print(s)

返回值:{1, 2, 3, 'd', 'b', 'c', 'e'}

猜你喜欢

转载自blog.csdn.net/maergaiyun/article/details/82227249