2024年最全python入门之python开发笔记基本数据类型集合(1)

image

a = {1,2,3}

b = {2,3,4,5}

print(a.symmetric_difference(b))

print(a^b)

包含关系

in,not in:判断某元素是否在集合内

==,!=:判断两个集合是否相等

两个集合之间一般有三种关系,相交、包含、不相交。在Python中分别用下面的方法判断:

  • set.isdisjoint(s):判断两个集合是不是不相交

  • set.issuperset(s):判断集合是不是包含其他集合,等同于a>=b

  • set.issubset(s):判断集合是不是被其他集合包含,等同于a<=b

集合的常用操作


元素的增加

单个元素的增加 : add(),add的作用类似列表中的append

对序列的增加 : update(),而update类似extend方法,update方法可以支持同时传入多个参数:

a={1,2}

a.update([3,4],[1,2,7])

a

{1, 2, 3, 4, 7}

a.update(“hello”)

a

{1, 2, 3, 4, 7, ‘h’, ‘e’, ‘l’, ‘o’}

a.add(“hello”)

a

{1, 2, 3, 4, ‘hello’, 7, ‘h’, ‘e’, ‘l’, ‘o’}

元素的删除

集合删除单个元素有两种方法:

元素不在原集合中时:

set.discard(x)不会抛出异常

set.remove(x)会抛出KeyError错误

a={1,2,3,4}

a.discard(1)

a

{2, 3, 4}

a.discard(1)

a

{2, 3, 4}

a.remove(1)

Traceback (most recent call last):

File “”, line 1, in

KeyError: 1

pop():由于集合是无序的,pop返回的结果不能确定,且当集合为空时调用pop会抛出KeyError错误,

clear():清空集合

a={3,“a”,2.1,1}

a.pop()

a.pop()

a.clear()

a

set()

a.pop()

Traceback (most recent call last):

File “”, line 1, in

KeyError: ‘pop from an empty set’

集合的工厂函数


class set(object):

“”"

set() -> new empty set object

set(iterable) -> new set object

Build an unordered collection of unique elements.

“”"

def add(self, *args, **kwargs): # real signature unknown

“”"

Add an element to a set.

This has no effect if the element is already present.

“”"

pass

def clear(self, *args, **kwargs): # real signature unknown

“”" Remove all elements from this set. “”"

pass

def copy(self, *args, **kwargs): # real signature unknown

“”" Return a shallow copy of a set. “”"

pass

def difference(self, *args, **kwargs): # real signature unknown

“”"

相当于s1-s2

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.)

“”"

pass

def difference_update(self, *args, **kwargs): # real signature unknown

“”" Remove all elements of another set from this set. “”"

pass

def discard(self, *args, **kwargs): # real signature unknown

“”"

与remove功能相同,删除元素不存在时不会抛出异常

Remove an element from a set if it is a member.

If the element is not a member, do nothing.

“”"

pass

def intersection(self, *args, **kwargs): # real signature unknown

“”"

相当于s1&s2

Return the intersection of two sets as a new set.

(i.e. all elements that are in both sets.)

“”"

pass

def intersection_update(self, *args, **kwargs): # real signature unknown

“”" Update a set with the intersection of itself and another. “”"

pass

def isdisjoint(self, *args, **kwargs): # real signature unknown

“”" Return True if two sets have a null intersection. “”"

pass

def issubset(self, *args, **kwargs): # real signature unknown

“”"

相当于s1<=s2

Report whether another set contains this set. “”"

pass

def issuperset(self, *args, **kwargs): # real signature unknown

“”"

相当于s1>=s2

Report whether this set contains another set. “”"

pass

def pop(self, *args, **kwargs): # real signature unknown

“”"

Remove and return an arbitrary set element.

Raises KeyError if the set is empty.

“”"

pass

def remove(self, *args, **kwargs): # real signature unknown

“”"

Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError.

“”"

pass

def symmetric_difference(self, *args, **kwargs): # real signature unknown

“”"

相当于s1^s2

Return the symmetric difference of two sets as a new set.

(i.e. all elements that are in exactly one of the sets.)

“”"

pass

def symmetric_difference_update(self, *args, **kwargs): # real signature unknown

“”" Update a set with the symmetric difference of itself and another. “”"

pass

def union(self, *args, **kwargs): # real signature unknown

“”"

相当于s1|s2

Return the union of sets as a new set.

最后

Python崛起并且风靡,因为优点多、应用领域广、被大牛们认可。学习 Python 门槛很低,但它的晋级路线很多,通过它你能进入机器学习、数据挖掘、大数据,CS等更加高级的领域。Python可以做网络应用,可以做科学计算,数据分析,可以做网络爬虫,可以做机器学习、自然语言处理、可以写游戏、可以做桌面应用…Python可以做的很多,你需要学好基础,再选择明确的方向。这里给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

👉Python所有方向的学习路线👈

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

👉Python必备开发工具👈

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

👉Python全套学习视频👈

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

👉实战案例👈

学python就与学数学一样,是不能只看书不做题的,直接看步骤和答案会让人误以为自己全都掌握了,但是碰到生题的时候还是会一筹莫展。

因此在学习python的过程中一定要记得多动手写代码,教程只需要看一两遍即可。

👉大厂面试真题👈

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值