Set is similar to Dict and is a set of keys, but does not store value
>>> s = set([1, 2, 3])>>> s{1, 2, 3}
Note that the parameter passed in [1, 2, 3]
is a list, and the display {1, 2, 3}
only tells you that the set has 3 elements, the order of the display does not indicate that the set is ordered.
Repeating elements are automatically filtered in set:
>>> s = set([1, 1, 2, 2, 3, 3])>>> s{1, 2, 3}
You can add(key)
add elements to a set by means of a method, and you can add them repeatedly, but with no effect:
>>> s.add(4)>>> s{1, 2, 3, 4}>>> s.add(4)>>> s{1, 2, 3, 4}
Repeating elements are automatically filtered in set:
>>> s = set([1, 1, 2, 2, 3, 3])>>> s{1, 2, 3}
You can add(key)
add elements to a set by means of a method, and you can add them repeatedly, but with no effect:
>>> s.add(4)>>> s{1, 2, 3, 4}>>> s.add(4)>>> s{1, 2, 3, 4}
remove(key)
You can delete an element by means of:
>>> s.remove(4)>>> s{1, 2, 3}
Set can be regarded as a set of unordered and non-repeating elements in mathematical sense, so two sets can do the intersection and set of mathematical meanings, and so on:
>>> s1 = set([1, 2, 3])>>> s2 = set([2, 3, 4])>>> s1 & s2{2, 3}>>> s1 | s2{1, 2, 3, 4}
set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象
Liaoche PY Note 5 set