[python]-set

Liao Xuefeng from teachers blog
to create a set, need a list as a collection

>>> s = set([1,2,3,4,3,2,1])
>>> s
{1, 2, 3, 4}

Add a list of the time, an error

>>> s.add(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> a
[1, 2, 3]

Add a tuple (does not include the list), you can

>>> b = (1,2,3)
>>> b
(1, 2, 3)
>>> s
{1, 2, 3, 5}
>>> s.add(b)
>>> s
{1, 2, 3, 5, (1, 2, 3)}

Add a tuple (containing list), error

>>> c = (1,[2,3])
>>> s.add(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

The only difference is that only a dict set and a corresponding value is not stored, and the principles set dict, same as mutable objects may not be placed, since the two variable object determines whether or not equal, there will be no guarantee that house set repeating elements.

Published 35 original articles · won praise 10 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_34358193/article/details/103124795