python入门之python开发笔记基本数据类型集合_python定义一个空集合,用于存储学生姓名(1)

最后

🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

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

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


![image](https://img-blog.csdnimg.cn/img_convert/5a86b28370253be925a3bab9d567637a.png)


### 集合的定义



l= {1,2,3,1} #此处应说明集合“去重”的效果
#定义可变集合

set_test=set(‘hello’) #此处应说明集合的“无序性”
set_test
{‘l’, ‘o’, ‘e’, ‘h’}
#改为不可变集合frozenset
f_set_test=frozenset(set_test)
f_set_test
frozenset({‘l’, ‘e’, ‘h’, ‘o’})


### 集合的**关系运算**


除了刚刚我们学过的交集之外,集合还可以做其他的关系运算。


继续以引子为例,现在已知两个集合分别是学习linux班的同学和学习python班的同学


&.&=:交集——既学习linux课程也学习python课程的同学


![image](https://img-blog.csdnimg.cn/img_convert/a625d17c9f0b547dda722df7317199bb.png)



l= {‘张三’,‘李四’,‘老男孩’}
p = {‘张三’,‘李四’,‘alex’}
print(l.intersection§)
print(l&p)


|,|=:合集,也叫并集——linux班和python班的所有同学



> 
> 这里同学们不可以单纯的认为python班的人数+linux班的人数就是结果,如果两班人数相加应该是6人。但学习Linux班的张三和李四还同时在python班学习,因此我们实际上总共只有四个学员。**求并集除了合并效果之外还有去重功能**
> 
> 
> 


![image](https://img-blog.csdnimg.cn/img_convert/1de3238223442d32de566832de713c73.png)



l= {‘张三’,‘李四’,‘老男孩’}
p = {‘张三’,‘李四’,‘alex’}
print(l.union§)
print(l|p)


-,-=:差集——只在linux而不python班的同学


![image](https://img-blog.csdnimg.cn/img_convert/1b2282afda9658ce57ba6f42530c5954.png)



l= {‘张三’,‘李四’,‘老男孩’}
p = {‘张三’,‘李四’,‘alex’}
print(l.difference§)
print(l-p)


,=:对称差集——只在linux班或只在python班的同学


![image](https://img-blog.csdnimg.cn/img_convert/16fff984f16dd3db67ff56a94119214d.png)



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.

在这里插入图片描述

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

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

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

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值