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

[Python] use of set set

編輯:Python

set Created collection ​ It's a set of elements that don't repeat in disorder , Relationship testing is possible , Delete duplicate data , You can also compute the intersection 、 Difference set 、 And set etc. .​

Set operator

Operation Python Operator meaning Example intersection & Take two sets of common elements (intersection)>>> set1 & set2
{3} Combine | Take two sets of all elements (union)>>> set1 | set2
{1,2,3,4,5} Difference set - Take elements that one set does not have in another set (difference)>>> set1 - set2
{1,2}
>>> set2 - set1
{4,5} Symmetric difference set ^ Take set A and B It doesn't belong to A&B The elements of (symmetric_difference)>>> set1 ^ set2
{1,2,4,5}

Add elements to the collection :

a = {"1", "2", "3"}
a.add("qwq")
print(a)

 

Like adding elements from a list to a collection :

a = {"1", "2", "3"}
a.update(["qwq","QAQ"])
print(a)

But if update If a single string is written in it, it will be split

a = {"1", "2", "3"}
a.update("QAQ")
print(a)

 

Get the elements unique to each of the two collections :

a = {"1", "2", "3"}
b = {"1", "4"}
ret = a.symmetric_difference(b)

Delete elements from the collection  

a = {"1", "2", "3"}
a.remove("1")
print(a)

 

 


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