author : Han Xinzi @ShowMeAI
Tutorial address :http://www.showmeai.tech/tutorials/56
This paper addresses :http://www.showmeai.tech/article-detail/80
Statement : copyright , For reprint, please contact the platform and the author and indicate the source
aggregate (set) Is an unordered sequence of non-repeating elements .
You can use braces { } perhaps set() Function to create a collection , Be careful : To create an empty collection, you must use the set() instead of { }, because { } Is used to create an empty dictionary .
Create format :
parame = {value01,value02,...} perhaps set(value)
Here is an example code ( The code can be in On-line python3 Environmental Science Run in ):
>>> company = {'Baidu', 'ShowMeAI', 'google', 'ByteDance', 'ShowMeAI', 'Taobao', 'Tencent'} >>> print(company) # Here is the de duplication function {'Baidu', 'ShowMeAI', 'google', 'ByteDance', 'Taobao', 'Tencent'} >>> 'Baidu' in basket # Quickly determine whether the element is in the set True >>> 'Meituan' in basket False
>>> # The operation between two sets is shown below . ... >>> a = set('abracadabra') >>> b = set('alacazam') >>> a {'a', 'r', 'b', 'c', 'd'} >>> a - b # aggregate a Contains and sets b Elements not contained in {'r', 'd', 'b'} >>> a | b # aggregate a or b All elements contained in {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # aggregate a and b All of them contain elements of {'a', 'c'} >>> a ^ b # Not also included in a and b The elements of {'r', 'd', 'b', 'm', 'z', 'l'}
Similar list derivation , Similarly, set supports set derivation (Set comprehension):
>>> a = {x for x in 'abracadabra' if x not in 'abc'} >>> a {'r', 'd'}
The syntax is as follows :
s.add( x )
Put the element x Add to collection s in , If the element already exists , Nothing is done .
>>> company = set(("Google", "ShowMeAI", "Taobao")) >>> company.add("Facebook") >>> print(company) {'Taobao', 'Facebook', 'Google', 'ShowMeAI'}
There's another way , You can also add elements , And the parameter can be a list , Tuples , Dictionary, etc , The syntax is as follows :
s.update( x )
x There can be multiple , Comma off . Here is an example code ( The code can be in On-line python3 Environmental Science Run in ):
>>> company = set(("Google", "ShowMeAI", "Taobao")) >>> company.update({"Facebook", "LinkedIn"}) >>> print(company) {'LinkedIn', 'Google', 'ShowMeAI', 'Facebook', 'Taobao'} >>> company.update([1,4],[5,6]) >>> print(company) {1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'}
The syntax is as follows :
s.remove( x )
Put the element x From the collection s Remove , If the element does not exist , An error will occur . Here is an example code ( The code can be in On-line python3 Environmental Science Run in ):
>>> company = set(("Google", "ShowMeAI", "Taobao")) >>> company.remove("Taobao") >>> print(company) {'Google', 'ShowMeAI'} >>> company.remove("Facebook") # No errors will occur Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'Facebook'
There is also a way to remove elements from the collection , And if the element does not exist , There will be no errors . The format is as follows :
s.discard( x )
>>> company = set(("Google", "ShowMeAI", "Taobao")) >>> company.discard("Facebook") # No, no error occurs >>> print(company) {'Taobao', 'Google', 'ShowMeAI'}
We can also set to randomly delete an element in the collection , The syntax is as follows :
s.pop()
company = set(("Google", "ShowMeAI", "Taobao", "Facebook")) x = company.pop() print(x)
Output results :
ShowMeAI
The results of multiple tests are different .
set A collection of pop Method will arrange the collection disorderly , Then delete the first element on the left of the unordered set .
The syntax is as follows :
len(s)
Computing sets s Element number .
company = set(("Google", "ShowMeAI", "Taobao", "Facebook")) print(len(company))
The syntax is as follows :
s.clear()
Empty the set s.
company = set(("Google", "ShowMeAI", "Taobao", "Facebook")) company.clear()
The syntax is as follows :
x in s
Element of judgement x Is it gathering s in , There is returned True, There is no return False.
company = set(("Google", "ShowMeAI", "Taobao", "Facebook")) "Facebook" in company
Method
describe
add()
Add an element to the collection
clear()
Remove all elements from the collection
copy()
Copy a collection
difference()
Returns the difference set of multiple sets
difference_update()
Remove elements from collection , This element also exists in the specified set .
discard()
Delete the elements specified in the collection
intersection()
Returns the intersection of sets
intersection_update()
Returns the intersection of sets .
isdisjoint()
Determine whether two sets contain the same elements , If there is no return True, Otherwise return to False.
issubset()
Determine whether the specified set is a subset of the method parameter set .
issuperset()
Determine whether the parameter set of the method is a subset of the specified set
pop()
Remove elements at random
remove()
Removes the specified element
symmetric_difference()
Returns the set of elements that are not repeated in two sets .
symmetric_difference_update()
Remove the same elements from the current collection in another specified collection , And insert different elements of another specified collection into the current collection .
union()
Returns the union of two sets
update()
Add elements to the collection
Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition
https://www.bilibili.com/video/BV1yg411c7Nw
The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , Babies who can surf the Internet scientifically can also use google colab One click operation and interactive operation learning Oh !
This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :