程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Graphical Python | set

編輯:Python
ShowMeAI research center

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


1.Python aggregate

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 .

aggregate (Set) Of 4 A feature

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'}

2. The basic operation of a set

An instance of a collection
aggregate (Set) Basic operation

(1) Additive elements

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'}

(2) Remove elements

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 .

(3) Calculate the number of set elements

The syntax is as follows :

len(s)

Computing sets s Element number .

company = set(("Google", "ShowMeAI", "Taobao", "Facebook"))
print(len(company))

(4) Empty the set

The syntax is as follows :

s.clear()

Empty the set s.

company = set(("Google", "ShowMeAI", "Taobao", "Facebook"))
company.clear()

(5) Determine whether the element exists in the collection

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

(6) Set built-in method complete list

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

3. Video tutorial

Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition

https://www.bilibili.com/video/BV1yg411c7Nw

Data and code download

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 :

  • Python Quick reference table

Expand references

  • Python course —Python3 file
  • Python course - Liao Xuefeng's official website

ShowMeAI Recommended articles

  • python Introduce
  • python Installation and environment configuration
  • python Basic grammar
  • python Basic data type
  • python Operator
  • python Condition control and if sentence
  • python Loop statement
  • python while loop
  • python for loop
  • python break sentence
  • python continue sentence
  • python pass sentence
  • python String and operation
  • python list
  • python Tuples
  • python Dictionaries
  • python aggregate
  • python function
  • python Iterators and generators
  • python data structure
  • python modular
  • python File read and write
  • python File and directory operations
  • python Error and exception handling
  • python object-oriented programming
  • python Namespace and scope
  • python Time and date

ShowMeAI A series of tutorials are recommended

  • The illustration Python Programming : From introduction to mastery
  • Graphical data analysis : From introduction to mastery
  • The illustration AI Mathematical basis : From introduction to mastery
  • Illustrate big data technology : From introduction to mastery
showmeai.tech

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved