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

Python data types -- collections

編輯:Python

Set characteristics : disorder , Do not modify the , Self weight removal , So you can set() Defining data as a set can achieve the function of de duplication .

a={1,3,2,2,3}
print(a)
b=[1,1,3,3,2,2]
print(set(b))
Return results :
{1, 2, 3}
{1, 2, 3}

newly added , Delete

a={1,2,3}
a.discard(1) # Delete the specified value
a.add(4) # New value added
b = a.pop() # Randomly delete and return
print(a,b)
Return results :
{3, 4} 2

intersection , Combine , Difference set

a={1,2,3}
b={3,4,5}
print(a.union(b)) # Union
print(a.intersection(b)) # Find the intersection
print(a.difference(b)) # Difference set ,a Yes b No,
print(a.symmetric_difference(b)) # Eliminate intersection
print(a.issubset(b)) # Judge a yes b Subset
print(a.issuperset(b)) # Judge a yes b A set of parent
Return results :
{1, 2, 3, 4, 5}
{3}
{1, 2}
{1, 2, 4, 5}
False
False


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