Python设置操作。

在数学中,集合是不同对象的集合,这些对象本身就是对象。例如,数字2、4和6在分别考虑时是不同的对象,但是当将它们一起考虑时,它们形成一个由{2,4,6}组成的单一大小三集。

集合操作

运作方式符号含义
路口A∩B同时存在的所有元素和
联盟A∪B或(或两者)中的所有元素
区别A − B所有但不在其中的所有元素
补充(要么)所有不在的元素

与列表相比,在python中,使用集合的主要优点是它具有优化的功能,可以检查特定元素是否为集合的成员。这基于哈希表数据结构。

集合方法

add(x)方法:如果不存在项x,则将项x添加到集合中。

A = {"AA", "BB", "CC"}
A.add("VV")

这将在A组中添加VV。

union(s)方法:返回两个集合的并集。在两个现有集合之间使用运算符'|'与编写My_Set1.union(My_Set2)相同。

A = {"AA", "BB", "CC"}
B = {"MM", "NN"}
Z = A.union(B)
OR
Z = A|B

集人口集将具有A和B的成分。

intersect(s)方法:返回两个给定集的交集。可以在此操作中使用“&”运算符。

S = A.intersection(B)

集合受害者将包含A和B的共同要素。

差异方法:返回一个集合,其中包含第一个集合中存在但第二个集合中不存在的所有元素。我们可以在这里使用“-”运算符。

W = A.difference(B)
OR
S = A – B

设置安全将具有A中的所有元素,但B中没有。

clear()方法:现有的整个集合将为空。

B.clear()

清除B套

集运算符

集和冻结集支持以下运算符-

key in s         # containment check
key not in s   # non-containment check
s1 == s2       # s1 is equivalent to s2
s1 != s2       # s1 is not equivalent to s2
s1 <= s2    # s1is subset of s2 s1 < s2     # s1 is proper subset of s2 s1 >= s2             # s1is superset of s2
s1 > s2     # s1 is proper superset of s2
s1 | s2        # the union of s1 and s2
s1 & s2        # the intersection of s1 and s2
s1 – s2        # the set of elements in s1 but not s2
s1 ˆ s2        # the set of elements in precisely one of s1 or s2

范例程式码

# Python program to demonstrate working# of 
# Set in Python 
# creating two sets 
My_Set1 = set()My_Set2 = set()# Adding elements to My_Set1
for i in range(1, 6): 
   My_Set1.add(i) 
# Adding elements to My_Set2
for i in range(3, 8): 
   My_Set2.add(i) 
print("My_Set1 = ", My_Set1) 
print("My_Set2 = ", My_Set2) 
print("\n") 
# Union of My_Set1 and My_Set2
My_Set3 = My_Set1 | My_Set2# My_Set1.union(My_Set2) 
print("Union of My_Set1&My_Set2: My_Set3 = ", My_Set3) 
# Intersection of My_Set1 and My_Set2
My_Set4 = My_Set1&My_Set2# My_Set1.intersection(My_Set2) 
print("Intersection of My_Set1&My_Set2: My_Set4 = ", My_Set4) 
print("\n") 
# Checking relation between My_Set3 and My_Set4
if My_Set3>My_Set4: # My_Set3.issuperset(My_Set4) 
   print("My_Set3 is superset of My_Set4") 
elif My_Set3<My_Set4: # My_Set3.issubset(My_Set4) 
   print("My_Set3 is subset of My_Set4") 
else : # My_Set3 == My_Set4
   print("My_Set3 is same as My_Set4") 
# displaying relation between My_Set4 and My_Set3
if My_Set4<My_Set3: # My_Set4.issubset(My_Set3) 
   print("My_Set4 is subset of My_Set3") 
   print("\n") 
# difference between My_Set3 and My_Set4
My_Set5 = My_Set3 - My_Set4
print("Elements in My_Set3 and not in My_Set4: My_Set5 = ", My_Set5) 
print("\n") 
# check if My_Set4 and My_Set5 are disjoint sets 
if My_Set4.isdisjoint(My_Set5): 
   print("My_Set4 and My_Set5 have nothing in common\n") 
# Removing all the values of My_Set5
My_Set5.clear()
print("After applying clear on sets My_Set5: ") 
print("My_Set5 = ", My_Set5)

输出结果

My_Set1 = {1, 2, 3, 4, 5}
My_Set2 = {3, 4, 5, 6, 7}
Union of My_Set1&My_Set2: My_Set3 = {1, 2, 3, 4, 5, 6, 7}
Intersection of My_Set1&My_Set2: My_Set4 = {3, 4, 5}
My_Set3 is superset of My_Set4
My_Set4 is subset of My_Set3
Elements in My_Set3 and not in My_Set4: My_Set5 = {1, 2, 6, 7}
My_Set4 and My_Set5 have nothing in common
After applying clear on sets My_Set5: 
My_Set5 = set()