Python Study Notes Set [05]

set is unordered, no duplicate set of elements.

Similar dict and set, a set key is set, but not stored value. Since the key can not be repeated, so that, in the set, no duplicate key.

 By creating a set list, list the filter element is repeated

2 by the remove(key)method can remove elements:

3 by the add(key)method elements may be added to the set may be repeated to add, but has no effect

4 set intersection and union can do the calculation

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4]) >>> s1 & s2 {2, 3} >>> s1 | s2 {1, 2, 3, 4}

 The elements are set. 5 immutable, t2 is variable in the list element can not be placed in the set

>>> t1 = (1,2,3)

>>> t2 = (1,[2,3])

 

>>> s1 = set()

>>> type(s1)

<class 'set'>

 

>>> s1.add(t1)

>>> s1

{(1, 2, 3)}

 

>>> s1.add(t2)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: unhashable type: 'list'

Guess you like

Origin www.cnblogs.com/wooluwalker/p/12043792.html