Article Directory
-
-
- 1. Introduction
- 2. Collection basic operations
-
- 1. add() adds a single element
- 2. update() adds lists, tuples, dictionaries and other overall data
- 3. remove ( ) removes the element: the element does not exist, an error will be reported
- 4. discard() removes the element: the element does not exist, no error will be reported
- 5. pop() randomly deletes an element from the collection
- 6. len() calculates the number of collection elements
- 7. clear() to empty the collection
- 8. in determines whether an element exists in the collection
- 9. Combining operations with list lists
- 3. The set set is used for intersection, union, and difference operations
- 4. Complete list of collection built-in methods
- 5. Reference links
-
1. Introduction
A set (set) is an unordered sequence of non-repeating elements , which can be created using curly braces { } or the set() function . Note: to create an empty set, you must use set() instead of { }, because { } is used to Create an empty dictionary .
1. Create a set collection
Create a set collection:
parame = {
value01,value02,...}
or:
value = [valu1,value2,value3] #value 是可迭代对象, 可以是列表list,元组(tuple),或者字符串string等可迭代数据结构对象
set(value)
Example:
>>> basket = {
'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # 这里演示的是去重功能
{
'orange', 'banana', 'pear', 'apple'}
2. Determine whether the element is in the collection
>>> 'orange' in basket # 快速判断元素是否在集合内
True
>>> 'crabgrass' in basket
False
3. Set comprehension
Like list comprehensions, sets also support set comprehensions:
>>> a = {
x for x in 'abracadabra' if x not in 'abc'}
>>> a
{
'r', 'd'}
2. Collection basic operations
1. add() adds a single element
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.
Note: The elements added using the add() method can only be numbers, strings, tuples, or Boolean (True and False) values, and variable data such as lists, dictionaries, and sets cannot be added.
Example:
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.add("Facebook")
>>> print(thisset)
{
'Taobao', 'Facebook', 'Google', 'Runoob'}
2. update() adds lists, tuples, dictionaries and other overall data
update() can also add a single element, 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:
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.update({
1,3})
>>> print(thisset)
{
1, 3, 'Google', 'Taobao', 'Runoob'}
>>> thisset.update([1,4],[5,6])
>>> print(thisset)
{
1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'}
>>>
3. remove ( ) removes the element: the element does not exist, an error will be reported
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.
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.remove("Taobao")
>>> print(thisset)
{
'Google', 'Runoob'}
>>> thisset.remove("Facebook") # 不存在会发生错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Facebook'
>>>
4. discard() removes the element: the element does not exist, no error will be reported
There is another 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 )
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.discard("Facebook") # 不存在不会发生错误
>>> print(thisset)
{
'Taobao', 'Google', 'Runoob'}
5. pop() randomly deletes an element from the collection
You can also set to randomly delete an element in the collection, the syntax format is as follows:
s.pop()
Example:
thisset = set(("Google", "Runoob", "Taobao", "Facebook"))
x = thisset.pop()
print(x)
#Runoob
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 .
6. len() calculates the number of collection elements
The syntax format is as follows:
len(s)
For example, calculate the number of elements in the set s:
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> len(thisset)
3
7. clear() to empty the collection
Clear the set s, the syntax is as follows:
s.clear()
Example:
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.clear()
>>> print(thisset)
set()
8. in determines whether an 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:
>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> "Runoob" in thisset
True
>>> "Facebook" in thisset
False
>>>
9. Combining operations with list lists
Since elements can only be stored once in a set, a set (set) can be used to filter out duplicates from other sets, and the list can be directly converted into a set (set) and then converted back, which can remove the duplicates in the list repeating element:
L = [1,2,1,3,2,4,5]
>>> set(L)
set([1, 2, 3, 4, 5])
>>> L = list(set(L))
>>> L
[1, 2, 3, 4, 5]
3. The set set is used for intersection, union, and difference operations
The most common operations for sets are intersection, union, difference, and symmetric difference operations. Let me introduce the meaning of each operation.
In the figure above, there are 2 sets, namely set1={1,2,3} and set2={3,4,5}, which have both the same elements and different elements. Taking these two sets as examples, the results of different operations are shown in Table 1 below:
Example:
>>> # 下面展示两个集合间的运算.
>>> 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'}