1、集合(set)
集合是无序且无索引的集合。 在Python中,集合用大括号括起来。
例如:
创建一个集合:thisset = {"apple", "banana", "cherry"}
print(thisset)
注意:集合是无序的,因此您不能确定的元素将以什么顺序出现。
2、访问集合中元素
您不能通过引用索引或键来访问集合中的项目。
但是,您可以使用for循环遍历设置项,或者通过使用in关键字询问集合中是否存在指定值。
例如:
遍历集合,打印值:thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
例如:
检查集合中是否存在"banana":thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
3、修改集合元素
创建集后,您将无法更改其项目,但可以添加新项目。
4、添加元素
要将一个项目添加到集合中,请使用add()方法。
要向一个集合中添加多个项目,请使用update()方法。
例如:
使用add()方法将项目添加到集合中:thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
例如:
使用update()方法将多个项目添加到集合中:thisset = {"apple", "banana", "cherry"}
thisset.update(["orange", "mango", "grapes"])
print(thisset)
5、获取集合的长度
要确定集合中有多少项,请使用len()方法。
例如:
获取集合中的项目数:thisset = {"apple", "banana", "cherry"}
print(len(thisset))
6、删除集合中元素
要删除集合中的项目,请使用remove()或discard()方法。
例如:
使用remove()方法删除"banana":thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
注意:如果不存在要删除的项目,remove()将引发错误。
例如:
使用discard()方法删除"banana":thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
注意:如果不存在要删除的项目,discard()将不引发错误。
您还可以使用pop()方法来删除一个项目,但是这个方法将删除最后的项目。请记住,集合是无序的,因此您将不知道要删除的项是什么。
pop()方法的返回值是已删除的项目。
例如:
使用pop()方法删除最后一项:thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
注意:集合是无序的,因此,当使用pop()方法时,您将不知道要删除的项目。
例如:
clear()方法清空集合:thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
例如:
del关键字将完全删除该集合:thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
7、连接两个集合
有几种方法可以在Python中连接两个或多个集合。
可以使用union()方法返回包含两个集合中所有项的新集合,或者使用update()方法将一个集合中的所有项插入到另一个集合中:
例如:
union()方法返回一个新集合,其中包含两个集合中的所有项目:set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
例如:
update()方法将set2中的项插入到set1中:set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
注意:union()和update()都将排除所有重复项。
还有其他方法将两个集合连接在一起,并且仅保留重复项,或者永不重复,请查看此页面底部的set方法的完整列表。
8、set()集合构造函数
也可以使用set()构造函数进行设置。
例如:
使用set()构造函数创建一个集合:thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(thisset)
9、set集合方法
Python有一组内置方法,可在集合上使用。方法描述
add()将元素添加到集合中
clear()从集合中删除所有元素
copy()返回集合的副本
difference()Returns a set containing the difference between two or more sets
difference_update()删除此集合中还包含在另一个指定集合中的项目
discard()删除指定的项目
intersection()返回一个集合,即另外两个集合的交集
intersection_update()删除此集合中其他指定集合中不存在的项目
isdisjoint()返回两个集合是否相交
issubset()返回另一个集合是否包含此集合
issuperset()Returns whether this set contains another set or not
pop()从集合中删除一个元素
remove()删除指定的元素
symmetric_difference()返回具有两组对称差的一组
symmetric_difference_update()插入此集合和另一个中的对称差
union()返回一个包含集合并集的集合
update()使用此集合和其他集合的并集更新集合