python内置函数系列之set(二)
书接上回
python内置函数系列之set(一)
- discard(丢弃,删除)
介绍:
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
即:如果元素是成员,则从集合中删除它。如果元素不是成员,则不执行任何操作。
- 参数说明:
discard参数必须有且仅有一个,这个参数应该是调用者集合中的子对象,否则传入则不发生任何改变。 - 返回值:
不是一个set集合,只是当运行完这个函数时,会返回None,它是在调用者集合自身上进行操作并保存。
实例:
a = {1, 2, 3, (2, 2)}
# 打印返回值,及删除1的结果
print(a.discard(1), a)
# 删除2
a.discard(2)
print(a)
# 删除子对象(2,2)
a.discard((2, 2))
print(a)
'''结果:
None {2, 3, (2, 2)}
{3, (2, 2)}
{3}
'''
- intersection
介绍:
"""
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
"""
即:
将两个集合的交集作为新集合返回。 (即两个集合中的所有元素。)
- 参数说明:
两个或多个集合(其实也可以传元组或者列表,但必须保证所传参数中至少含有一个set集合),当参数中不传入集合时,会报错。 - 返回值:
一个新set集合对象。
实例:
a = {1, 2, 3}
b = (3, 4, 5)
c = {3, 4, 5}
d = [3, 4, 5]
# 传入两个集合
x = set.intersection(a, c)
# 传入一个集合,一个列表
m = set.intersection(a, d)
# 传入3个参数
n = set.intersection(a, b, c)
print(x, m, n)
'''结果:
{3} {3} {3}
'''
8.intersection_update
介绍:
""" Update a set with the intersection of itself and another."""
即:
更新调用者集合(或者第一个参数集合),使得这个集合内的元素是交集内的元素。注:更新而不是新建。
- 参数:
两个或多个集合(或者是列表,元组等)。
重点:调用intersection_update的对象必须是一个集合(或者当使用set调用该方法的时,第一个参数必须是一个集合,不传入集合时,会报错)。 - 返回值:
返回值不是一个新集合,而是以None为标志代表你成功调用了。该方法是在调用对象身上进行操作。
代码示例:
a = {1, 2, 3}
b = (3, 4, 5)
c = {3, 2, 5}
d = [2, 3, 4]
# 用set调用,第一个参数必须是一个集合。
x = set.intersection_update(a, c)
# 用集合对象调用,参数可以是元组、列表、集合等。
y = c.intersection_update(b, d)
print(x, y, a, c)
'''结果:
None None {2, 3} {3}
'''
9.isdisjoint(是不相交的)
介绍:
""" Return True if two sets have a null intersection. """
即:
如果两个集合不相交(不含相同的元素),则返回True。
- 参数:
直接用set函数调用时,只能传递两个参数,且第一个参数必须是一个集合,第二个参数可以是集合、列表、元组等;
使用集合对象调用时,只能传递一个参数,可以为集合、列表、参数等。
- 返回值
Ture: 代表两个集合不相交(无相同的子元素);
False:代表两个集合相交(有相同的子元素)。
注:调用此方法不会改变集合中的元素。
示例代码:
a = {1, 2, 3}
b = (4, 5)
c = {3, 2, 5}
d = [2, 3, 4]
x = set.isdisjoint(a, b)
y = a.isdisjoint(d)
print(x, y, a, b, d)
'''结果:
True False {1, 2, 3} (4, 5) [2, 3, 4]
'''
- issubset(子集)
介绍:
""" Report whether another set contains this set. """
即:
报告另一个集合是否包含这个集合。
- 参数:
直接用set函数调用时,只能传递两个参数,且第一个参数必须是一个集合,第二个参数可以是集合、列表、元组等;
使用集合对象调用时,只能传递一个参数,可以为集合、列表、参数等。 - 返回值
Ture:
代表调用者集合是参数集合的子集(调用者集合被包含在参数集合中);【简记为后者包含前者】
False:
代表调用者集合不是参数集合的子集(调用者集合没有被包含在参数集合中);
注:调用此方法不会改变集合中的元素。
示例代码:
a = {1, 2, 3}
b = (4, 5)
d = {4, 5}
e = [4, 5, 6]
x = set.issubset(d, e)
y = a.issubset(b)
print(x, y, a, b)
'''结果:
True False {1, 2, 3} (4, 5)
'''
11.issuperset
这个函数与上一个函数issubset的恰恰相反,即判断调用者集合是否包含参数集合。(这里不做具体示范)
12.pop
介绍:
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
即:
移除并返回任意集合元素。如果集合为空,则引发 KeyError。
- 参数:
不需要参数(这是随机移除元素的原因之一) - 返回值:
返回被删除的元素。
注:此方法会改变调用者集合的内部元素。
代码示例:
d = {(2, 3), 4, 5, "hello", "apple"}
y = d.pop() # 等价于 y = set.pop(d)
print(y, d)
x = set.pop(d)
print(x, d)
'''结果:
(2, 3) {'hello', 4, 5, 'apple'}
hello {4, 5, 'apple'}
'''
对比list中的pop方法:
在list中,pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
深入理解:
为什么是任意呢?原因在于集合的无序性,无法像List那样进行默认删除。
- remove
学了pop方法,那有没有可以指定删除元素的函数呢?当然有,就是remove函数。
介绍:
"""
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
"""
即:
从集合中移除一个元素;它必须是成员。如果元素不是成员,则引发 KeyError。
- 参数:
有且只有一个元素对象。 - 返回值:
操作成功,返回None。
代码示例:
d = {(2, 3), 4, 5, "hello", "apple"}
y = d.remove(5) # 等价于 y = set.remove(d, 5)
print(y, d)
x = set.remove(d, "apple")
print(x, d)
'''结果:
None {(2, 3), 4, 'apple', 'hello'}
None {(2, 3), 4, 'hello'}
'''
13.union
介绍:
"""
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
"""
即:
将集合的并集作为新集合返回。(即任一集合中的所有元素。)
简单来说,将所有集合的所有元素拼凑成一个新集合并返回。
- 参数:
一个或多个集合
-返回值:
一个新集合。
代码示例:
d = {(2, 3), 4, 5, "hello", "apple"}
a = [1, 2]
c = (8, 9)
y = d.union(a, c) # 等价于 y = set.union(d, a, c)
print(y)
'''结果:
{1, 2, 4, 5, 8, 9, 'hello', (2, 3), 'apple'}
'''
14.update
介绍:
""" Update a set with the union of itself and others. """
即:
用它自己和其他人的并集 更新一个集合。
这个方法与上一个方法的区别就是它作用于调用者集合,使得调用者集合更新。而上一个方法union则是创建了一个新对象集合储存并进行返回。
在这里不做过多的代码示例。请参考上一个函数。
15.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.)
"""
即:
将两个集合的对称差作为一个新集合返回。(即,恰好在其中一个集合中的所有元素。)
说人话:
返回两个集合组成的新集合,但会移除两个集合的重复元素。
更多的专业概念:“左右集差”
- 参数:
有且仅有一个集合参数。 - 返回值:
一个新集合。
代码示例:
d = {(2, 3), 4, 5, "hello", "apple"}
a = {(2, 3), 6, "hello"}
y = d.symmetric_difference(a) # 等价于 y = set.symmetric_difference(d, a)
print(y)
'''结果:
{4, 5, 'apple', 6}
'''
16.symmetric_difference_update
这个方法与上一个方法的区别就是它作用于调用者集合,使得调用者集合更新。而上一个方法symmetric_difference则是创建了一个新对象集合储存并进行返回。
在这里不做过多的代码示例。请参考上一个函数。
总结一下:
很多函数都类似于上面几个函数,只是略微有些不同,在学习过程中,我们只要能学会类比,举一反三,就会容易很多。