1、认识集合

定义:

s={1,2,3,4,5}

s=set("hello")

s=set(["steven","job","dave"])

用set和可迭代对象进行定义时,相当于执行了for循环

补充:集合属于可变类型,如果定义不可变类型的集合用frozenset(),

如s=frozenset("hello")

print(s)  #结果为:frozenset({'h', 'o', 'l', 'e'})

特点:

不同元素组成

无序

集合中的元素必须是不可变  (字符串,数字,元祖)

2、常用方法

.add("a")    #增加一个元素

.clear()      #清空

.copy()      #浅拷贝

.pop()       #随机删

.remove("job")       #指定删,删除元素不存在时会报错

.discard("job")       #指定删,删除元素不存在时不会报错

3、基本运算

交集,并集,差集

a_s={"steve","job","dave","max"}
b_s={"job","dave","mark"}
print(a_s,b_s) #求交集
print(a_s.intersection(b_s)) #写法1
print(a_s&b_s) #写法2 #求并集
print(a_s.union(b_s)) #写法1
print(a_s|b_s) #写法2 #求差集,相当于存在于左边,但不存在于右边的集合
print(a_s.difference(b_s)) #写法1
print(a_s-b_s) #写法2
print(b_s-a_s)

结果:

{'dave', 'steve', 'max', 'job'} {'dave', 'mark', 'job'}
{'dave', 'job'}
{'dave', 'job'}
{'job', 'dave', 'mark', 'steve', 'max'}
{'job', 'dave', 'mark', 'steve', 'max'}
{'steve', 'max'}
{'steve', 'max'}
{'mark'}

4、其他方法

a_s={"steve","job","dave","max"}
b_s={"job","dave","mark"}
c_s={"job","dave"} #交叉补集 ,交集的补集,相当于并集减去交集的部分的集合
print(a_s.symmetric_difference(b_s)) #写法1
print(a_s^b_s) #写法2
#结果为:{'mark', 'max', 'steve'} #.difference_update()
a_s.difference_update(b_s) # 相当于 a_s=a_s-b_s
print(a_s)
#结果为:{'max', 'steve'} #.isdisjoint() 判断有没有交集,没有交集返回true
print(a_s.isdisjoint(b_s))
#结果为:false #.issubset() 是否是某个集合的子集。相当于是s1<=s2
print(c_s.issubset(a_s)) #写法1
print(c_s<=a_s) #写法2
#.issuperset() 是否是某个集合的父集。相当于是s1>=s2
print(a_s.issuperset(c_s))
#结果为 true #.update() 可以增加多个值。可以传元祖,列表等可迭代对象
a_s.update(b_s)
print(a_s)
b_s.update(("bob","zhou"))
print(b_s)
#结果为:
#{'steve', 'job', 'max', 'dave', 'mark'}
#{'bob', 'mark', 'job', 'dave', 'zhou'}
"""
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
"""
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 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
"""
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
""" Report whether another set contains this set. """
pass def issuperset(self, *args, **kwargs): # real signature unknown
""" 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
"""
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
"""
Return the union of sets as a new set. (i.e. all elements that are in either set.)
"""
pass def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
passseset

set

python学习笔记_集合的定义和常用方法的更多相关文章

  1. Python学习笔记_我的参考网址

    Python学习笔记, 下面记录网上搜到的可参考的网址: 一.关于Tkinter 1.Python3中tkinter模块使用方法详解 https://blog.csdn.net/Fighting_Bo ...

  2. python学习笔记整理——集合 set

    python学习整理笔记--集合 set 集合的用途:成员测试和消除重复的条目,进行集合运算 注意:花括号或set()函数可以用于创建集合. 注意:若要创建一个空的集合你必须使用set(),不能用{} ...

  3. Python学习笔记-Day3-set集合操作

    set集合,是一个无序且不重复的元素集合.定义方式类似字典使用{}创建 目前我们学过的数据类型: 1.字符串(str),2.整型(int),3.浮点型(float),4,列表(list) 5.元组(t ...

  4. python学习笔记(集合的使用)

    集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...

  5. python学习笔记(7)——集合(set)

    关键词#1.定义:无序不重复元素集, 基本功能包括关系测试和消除重复元素. 2.关键词:类似dict.只有key无value.常用于集合类数学运算. 3.创建 s=set() #入参可以是list.t ...

  6. 【Java学习笔记】<集合框架>定义功能去除ArrayList中的重复元素

    import java.util.ArrayList; import java.util.Iterator; import cn.itcast.p1.bean.Person; public class ...

  7. Python学习笔记7—集合

    set 拥有类似 dict 的特点:可以用{}花括号来定义:其中的元素没有序列,也就是是非序列类型的数据;而且,set 中的元素不可重复,这就类似 dict 的键. >>> s1 = ...

  8. 【Python学习笔记】集合

    概述 集合的一般操作 内建函数进行标准操作集合 数学运算符进行标准操作集合 集合的应用 概述 python的集合(set)是无序不重复元素集,是一种容器.集合(set)中的元素必须是不可变对象,即可用 ...

  9. Python学习笔记 - function调用和定义

    调用函数: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 >>> abs(100) 100 >>> a ...

随机推荐

  1. sql server C#操作。原文在收藏页面

    C#操作SQL Server数据库   1.概述 2.连接字符串的写法 3.SqlConnection对象 4.SqlCommand对象 5.SqlDataReader对象 6.DataSet对象 7 ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-share

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. junit基础学习之-简介(1)

    JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1  JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...

  4. Django配置日志

    在settings里配置 # 日志配置 LOGGING = { # 是python的版本 'version': 1, # 是否禁用 'disable_existing_loggers': False, ...

  5. C语言预处理理论

    C语言预处理理论1.从源码到可执行文件的过程(1)源码.c->(编译)->elf可执行程序(2)源码.c->(编译)->目标文件.o->(链接)->elf可执行程序 ...

  6. dns、网关、IP地址,主要是配置resolv.conf\network\ifcfg-eth0

    Ubuntu sudo vi /etc/network/interfac   添加 dns-nameservers 192.168.1.254dns-search stonebean.com cent ...

  7. Pillow库的学习和使用

    1.encoder jpeg not available sudo apt-get install libjpeg-dev pip install -I pillow

  8. UVA 11235 RMQ算法

    上次的湘潭赛的C题,用线段树敲了下还是WA,不知道为何,我已经注意了处理相同数据,然后他们当时用的RMQ. 所以学了下RMQ,感觉算法思想是一样的,RMQ用了DP或者是递推,由单个数到2^k往上推,虽 ...

  9. 二分图匹配 最大匹配数+最大点覆盖 POJ 1469+POJ 3041

    最大匹配数就等于最大点覆盖,因为在图里面,凡是要覆盖的点必定是连通的,而最大匹配之后,若还有点没有覆盖到,则必定有新的匹配,与最大匹配数矛盾,如果去掉一些匹配,则必定有点没有覆盖到. POJ 1469 ...

  10. python可移植支持代码;用format.节省打印输出参数代码;math模块;

    1.多平台移植代码: #!/usr/bin/env python3 这一行比较特殊,称为 shebang 行,在 Python 脚本中,你应该一直将它作为第一行. 请注意行中的第一个字符是井号(#). ...