Article directory
foreword
Past review:
- Python Series Tutorial – Introduction to Python3 (1)
- Python Series Tutorials – Python3 Environment Construction (2)
- Python Series Tutorial – Python3 VScode (3)
- Python Series Tutorials – Basic Grammar of Python3 (4)
- Python Series Tutorial – Python3 Basic Data Types (5)
- Python Series Tutorials--Python3 Data Type Conversion (6)
- Python Series Tutorials--Python3 Derivation (7)
- Python Series Tutorials--Python3 Interpreter and Annotations (8)
- Python series of tutorials--Python3 what is an operator (9)
- Python Series Tutorials--Python3 Numbers (Number) (10)
- Python Series Tutorials--Python3 Strings (11)
- Python Series Tutorials--Python3 List (12)
- Python Series Tutorials--Python3 List (13)
- Python Series Tutorials--Python3 Dictionary (14)
A collection (set) is an unordered sequence of elements that do not repeat.
You can use braces { } or the set() function to create a collection. Note: To create an empty collection, you must use set() instead of { }, because { } is used to create an empty dictionary.
Create format:
parame = {
value01,value02,...}
或者
set(value)
Example (Python 3.0+)
>>> basket = {
'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # 这里演示的是去重功能
{
'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # 快速判断元素是否在集合内
True
>>> 'crabgrass' in basket
False
>>> # 下面展示两个集合间的运算.
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
{
'a', 'r', 'b', 'c', 'd'}
>>> a - b # 集合a中包含而集合b中不包含的元素
{
'r', 'd', 'b'}
>>> a | b # 集合a或b中包含的所有元素
{
'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # 集合a和b中都包含了的元素
{
'a', 'c'}
>>> a ^ b # 不同时包含于a和b的元素
{
'r', 'd', 'b', 'm', 'z', 'l'}
Like list comprehensions, sets also support set comprehensions:
Example (Python 3.0+)
>>> a = {
x for x in 'abracadabra' if x not in 'abc'}
>>> a
{
'r', 'd'}
Basic operations on collections
1. Add elements
The syntax format is as follows:
s.add( x )
Adds the element x to the set s, or does nothing if the element already exists.
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> thisset.add("Facebook")
>>> print(thisset)
{'Taobao', 'Facebook', 'Google', 'demo'}
has another method, which can also add elements, and the parameters can be lists, tuples, dictionaries, etc. The syntax format is as follows:
s.update( x )
There can be multiple x, separated by commas.
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> thisset.update({
1,3})
>>> print(thisset)
{
1, 3, 'Google', 'Taobao', 'demo'}
>>> thisset.update([1,4],[5,6])
>>> print(thisset)
{
1, 3, 4, 5, 6, 'Google', 'Taobao', 'demo'}
>>>
2. Remove elements
The syntax format is as follows:
s.remove( x )
Removes the element x from the set s, or throws an error if the element does not exist.
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> thisset.remove("Taobao")
>>> print(thisset)
{
'Google', 'demo'}
>>> thisset.remove("Facebook") # 不存在会发生错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Facebook'
>>>
In addition, there is a method that also removes the element in the collection, and if the element does not exist, no error will occur. The format is as follows:
s.discard( x )
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> thisset.discard("Facebook") # 不存在不会发生错误
>>> print(thisset)
{
'Taobao', 'Google', 'demo'}
我们也可以设置随机删除集合中的一个元素,语法格式如下:
s.pop()
脚本模式实例(Python 3.0+)
thisset = set(("Google", "demo", "Taobao", "Facebook"))
x = thisset.pop()
print(x)
Output result:
$ python3 test.py
demo
The test results are different for multiple executions.
The pop method of the set collection will arrange the collection unordered, and then delete the first element on the left of the unordered collection.
3. Calculate the number of set elements
The syntax format is as follows:
len(s)
Count the number of elements in the set s.
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> len(thisset)
3
4. Empty collection
The syntax format is as follows:
s.clear()
Empty collection s.
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> thisset.clear()
>>> print(thisset)
set()
5. Determine whether the element exists in the collection
The syntax format is as follows:
x in s
Determine whether the element x is in the set s, return True if it exists, and return False if it does not exist.
Example (Python 3.0+)
>>> thisset = set(("Google", "demo", "Taobao"))
>>> "demo" in thisset
True
>>> "Facebook" in thisset
False
>>>
Complete list of collection built-in methods
method | describe |
---|---|
add() | add elements to the set |
clear() | remove all elements from the set |
copy() | copy a collection |
difference() | Returns the difference of multiple sets |
difference_update() | Removes an element from a collection that also exists in the specified collection. |
discard() | removes the specified element from the collection |
intersection() | Returns the intersection of sets |
intersection_update() | Returns the intersection of sets. |
isdisjoint() | Determine whether the two collections contain the same element, if not return True, otherwise return False. |
issubset() | Determines whether the specified collection is a subset of the method's parameter collection. |
superset() | Determine whether the parameter set of the method is a subset of the specified set |
pop() | Randomly remove elements |
remove() | remove specified element |
symmetric_difference() | Returns a collection of unique elements from both collections. |
symmetric_difference_update() | Removes the same elements in the current collection that are in another specified collection, and inserts the different elements in the other specified collection into the current collection. |
union() | Returns the union of two collections |
update() | add elements to the collection |