|
## set.remove()
In [142]: s
Out[142]: {'a', 'b', 'j', 'x'}
In [143]: s.remove("a")
In [144]: s
Out[144]: {'b', 'j', 'x'}
In [151]: s.remove("S")
-----------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-151-332efdd48daa> in <module>()
----> 1 s.remove("S")
KeyError: 'S'
## set.pop()
In [153]: s = {1, 2, 3, 4}
In [154]: s.pop()
Out[154]: 1
In [155]: s
Out[155]: {2, 3, 4}
In [156]: s.pop(5)
-----------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-156-23a1c03efc29> in <module>()
----> 1 s.pop(5)
TypeError: pop() takes no arguments (1 given)
In [157]: s.pop()
Out[157]: 2
In [158]: s.pop()
Out[158]: 3
In [159]: s.pop()
Out[159]: 4
In [160]: s.pop()
-----------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-160-e76f41daca5e> in <module>()
----> 1 s.pop()
KeyError: 'pop from an empty set'
## set.discard()
In [165]: help(set.discard)
Help on method_descriptor:
discard(...)
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
In [166]: s = {1, 2, 3}
In [167]: s.discard(2)
In [168]: s.discard(1, 3)
-----------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-168-8702b734cbc4> in <module>()
----> 1 s.discard(1, 3)
TypeError: discard() takes exactly one argument (2 given)
In [169]: s.discard(2) # 元素不存在时,不会报错
In [170]: s
Out[170]: {1, 3}
In [32]: s.clear()
In [33]: s
Out[33]: set()
In [47]: del(s)
In [48]: s
-----------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-48-f4d5d0c0671b> in <module>()
----> 1 s
NameError: name 's' is not defined |
|
|