目录
集合的定义:
集合的表现形式:
{value1,value2,value3......}
空集合的定义方法:
set_data = set()
集合的特性:
- 是否有序:否
- 元素是否可以重复:否
放入集合的元素可以重复
集合的调用:
add:
add(...)
| Add an element to a set.
|
| This has no effect if the element is already present.
添加:向原集合中添加元素(若该元素已存在,则不会造成任何影响)
案例:定义一个集合,给其中添加一个2和9
set_data = {1, 2, 3, 6}
set_data.add(2)
set_data.add(9)
print(set_data)
输出结果:
clear():
clear(...)
| Remove all elements from this set.
清空:清空集合中的所有元素
案例:定义一个集合,并且清空里面所有的元素
set_data = {1, 2, 3, 6}
set_data.clear()
print(set_data)
输出结果:
copy:
copy(...)
| Return a shallow copy of a set.
拷贝:返回值为一个浅拷贝的集合
案例:定义一个集合,拷贝他其中的元素
set_data = {1, 2, 3, 6}
print(set_data.copy())
输出结果:
difference():
difference(...)
| Return the difference of two or more sets as a new set.
|
| (i.e. all elements that are in this set but not the others.)
不同:将两个或者多个集合的不同之处以一个新的集合输出
案例:定义两个集合,输出他们的不同之处
set_data = {1, 2, 3, 6}
set_data2 = {2, 3, 4, 5}
print(set_data.difference(set_data2))
输出结果:
difference_update():
difference_update(...)
| Remove all elements of another set from this set.
不同点更新:difference_update() 方法与 difference() 方法不同,因为 difference() 方法返回一个新集合,其中没有不需要的项目,而 difference_update() 方法从原始集中删除了不需要的项目
案例:定义两个集合,以原集合输出他们的不同之处
set_data = {1, 2, 3, 6}
set_data2 = {2, 3, 4, 5}
set_data.difference_update(set_data2)
print(set_data)
输出结果:
discard():
discard(...)
| Remove an element from a set if it is a member.
|
| Unlike set.remove(), the discard() method does not raise
| an exception when an element is missing from the set.
移除:从原集合中移除一个元素
案例:定义一个集合,从中移除它的2
set_data = {1, 2, 3, 6}
set_data.discard(2)
print(set_data)
输出结果:
intersection():
intersection(...)
| Return the intersection of two sets as a new set.
|
| (i.e. all elements that are in both sets.)
交集:以一个新集合的形式返回两个集合的交集
案例:定义两个集合,输出他们的交集
set_data = {1, 2, 3, 6}
set_data2 = {2, 4, 0, 3, 9}
print(set_data.intersection(set_data2))
输出结果:
intersection_update():
intersection_update(...)
| Update a set with the intersection of itself and another.
交集更新:在原有的集合中更新为两个集合的交集
案例:定义两个集合,将原来的集合转变为两集合的交集
set_data = {1, 2, 3, 6}
set_data2 = {2, 4, 0, 3, 9}
print(set_data.intersection_update(set_data2))
print(set_data)
输出结果:
isdisjoint():
isdisjoint(...)
| Return True if two sets have a null intersection.
判断交集:若两个集合没有交集则返回True
案例:定义两个集合,判断他俩是否有交集
set_data = {1, 2, 3, 6}
set_data2 = {2, 4, 0, 3, 9}
print(set_data.isdisjoint(set_data2))
输出结果:有交集
issubset():
issubset(...)
| Report whether another set contains this set.
判断是否为子集:子集.issubset(父集)
案例:定义一个集合{1, 2},判断它是否为{1, 2, 3, 4}的子集
set_data = {1, 2}
set_data2 = {1, 2, 3, 4}
print(set_data.issubset(set_data2))
输出结果:
issuperset():
issuperset(...)
| Report whether this set contains another set.
判断是否为父集:父集.issuperset(子集)
案例:定义一个集合{1, 2, 3, 4},判断它是否为{1, 2}的父集
set_data = {1, 2}
set_data2 = {1, 2, 3, 4}
print(set_data2.issuperset(set_data))
输出结果:
pop():
pop(...)
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty.
在原列表中随机移除一个元素
案例:定义一个列表,并随机移除其中一个值
set_data = {1, 2, 3, 4}
print(set_data.pop())
print(set_data)
输出结果:
remove():
remove(...)
| Remove an element from a set; it must be a member.
|
| If the element is not a member, raise a KeyError.
remove() 方法用于在集合中移除集合中的指定元素
例如:定义一个集合,一出其中的元素2
set_data = {1, 2, 3, 4}
set_data.remove(2)
print(set_data)
输出结果:
symmetric_difference()
symmetric_difference(...)
| Return the symmetric difference of two sets as a new set.
|
| (i.e. all elements that are in exactly one of the sets.)
返回两个集合中不重复的元素集合
案例:定义两个集合,输出他们俩之间的差值
set_data = {1, 2}
set_data2 = {1, 2, 3, 4}
print(set_data.symmetric_difference(set_data2))
输出结果:
symmetric_difference_update()
symmetric_difference_update(...)
| Update a set with the symmetric difference of itself and another.
将原集合中的元素更新为两个集合的差值
案例:定义两个集合,将set_data 中的元素更新为两个集合的差值
set_data = {1, 2}
set_data2 = {1, 2, 3, 4}
set_data.symmetric_difference_update(set_data2)
print(set_data)
输出结果:
union()
union(...)
| Return the union of sets as a new set.
|
| (i.e. all elements that are in either set.)
返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次
案例:定义两个集合,输出他们的并集
set_data = {1, 2}
set_data2 = {1, 2, 3, 4}
print(set_data.union(set_data2))
输出结果:
update()
update(...)
| Update a set with the union of itself and others.
更新:可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略。
案例:定义一个集合set_data用另一个集合set_data2更新它
set_data = {1, 2}
set_data2 = {5, 8}
set_data.update(set_data2)
print(set_data)
输出结果: