python中,集合(set)是一个无序排列,可哈希,

支持集合关系测试,不支持索引和切片操作,没有特定语法格式,

只能通过工厂函数创建.集合里不会出现两个相同的元素,

所以集合常用来对字符串或元组或列表中的元素进行去重操作。

生成一个集合可以使用如下语法:

生成集合语法1:

>>> l1=[1,2,3,4,5,6]

>>> s1=set(l1)

>>> print(s1)

{1, 2, 3, 4, 5, 6}

在这里,使用工厂函数set创建集合,set的参数可以是一个列表,也可以是一个元组或字符串。

生成集合语法2:

>>> s2={6,7,8,9,10}

>>> print(s2)

{8, 9, 10, 6, 7}

生成集合语法3:

>>> s3={i for i in range(10)}

>>> print(s3)

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

集合类型的方法和操作:

add

为集合增加一个元素,如果集合中本来已经存在这个元素对集合无影响
Add an element to a set.
This has no effect if the element is already present.
>>> s1={1,2,3,4,5,6,7}
>>> s1.add(8)
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7, 8}
>>> s1.add(9)
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

clear

清空集合里所有的元素
Remove all elements from this set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9}
>>> s1.clear()
>>> print(s1)
set()
>>> s2.clear()
>>> print(s2)
set()

copy

对集合进行浅拷贝(只复制元素,不复制内存地址)
Return a shallow copy of a set.
>>> s1={1,2,3,4,5,6,7}
>>> print(s1,id(s1))
{1, 2, 3, 4, 5, 6, 7} 140509859430472
>>> s2=s1.copy()
>>> print(s2,id(s2))
{1, 2, 3, 4, 5, 6, 7} 140509842716712

difference

求两个或多个集合的差集,并返回一个新集合
Return the difference of two or more sets as a new set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.difference(s2)
{1, 2, 3, 4}
>>> s2.difference(s1)
{8, 9, 10}

difference_update

把两个集合的交集部分从集合中移除
Remove all elements of another set from this set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.difference_update(s2)
>>> print(s1)
{1, 2, 3, 4}
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s2.difference_update(s1)
>>> print(s2)
{8, 9, 10}

discard

从集合中移除一个元素,如果被移除的元素不在集合中,不会报错
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
{1, 2, 3, 4, 5, 6, 7}
>>> s1.discard(7)
>>> print(s1)
{1, 2, 3, 4, 5, 6}
>>> s1.discard(4)
>>> print(s1)
{1, 2, 3, 5, 6}
>>> print(s1)
{1, 2, 3, 5, 6}

intersection

求两个或多个集合中的交集
Return the intersection of two sets as a new set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.intersection(s2)
{5, 6, 7}
>>> s2.intersection(s1)
{5, 6, 7}

intersection_update

把两个集合的交集做为新的集合
Update a set with the intersection of itself and another.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.intersection_update(s2)
>>> print(s1)
{5, 6, 7}
>>> print(s2)
{5, 6, 7, 8, 9, 10} >>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s2.intersection_update(s1)
>>> print(s2)
{5, 6, 7}
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7}

isdisjoint

两个集合没有交集则返回True
Return True if two sets have a null intersection.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.isdisjoint(s2)
False
>>> s1={1,2,3,4}
>>> s2={6,7,8,9}
>>> s1.isdisjoint(s2)
True

issubset

如果本集合是参数集合的子集,返回True
Report whether another set contains this set.
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s1.issubset(s2)
True
>>> s2.issubset(s1)
False

issuperset

如果本集合是参数集合的超集,返回True
Report whether this set contains another set.
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s1.issuperset(s2)
False
>>> s2.issuperset(s1)
True

pop

从集合中移除一个元素,如果集合为空,则报错
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
>>> s1={2,3,4,5}
>>> s1.pop()
2
>>> print(s1)
{3, 4, 5}
>>> s1.pop()
3
>>> s1.pop()
4
>>> s1.pop()
5
>>> s1.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'

remove

移除集合中的一个元素,如果集合是空的,则报错
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
>>> s1={1,2,3,4,5,6}
>>> s1.remove(4)
>>> print(s1)
{1, 2, 3, 5, 6}
>>> s1.remove(9)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 9

symmetric_difference

返回两个集合的对称差集的集合
Return the symmetric difference of two sets as a new set.
>>> s1={1,2,3,4}
>>> s2={6,7,8,9}
>>> s1.symmetric_difference(s2)
{1, 2, 3, 4, 6, 7, 8, 9}
>>> s3={1,2,3,4,5,6}
>>> s4={5,6,7,8,9,10}
>>> s3.symmetric_difference(s4)
{1, 2, 3, 4, 7, 8, 9, 10}

symmetric_difference_update

与参数集合做对称差集,并返回给自身
Update a set with the symmetric difference of itself and another.
>>> s1={1,2,3,4}
>>> s2={6,7,8,9}
>>> s2.symmetric_difference_update(s1)
>>> print(s2)
{1, 2, 3, 4, 6, 7, 8, 9} >>> s3={1,2,3,4,5,6}
>>> s4={5,6,7,8,9,10}
>>> s3.symmetric_difference_update(s4)
>>> print(s3)
{1, 2, 3, 4, 7, 8, 9, 10}

union

求两个或多个集合的并集
Return the union of sets as a new set.
>>> s1={1,2,3,4,5,6}
>>> s2={5,6,7,8,9}
>>> s1.union(s2)
{1, 2, 3, 4, 5, 6, 7, 8, 9} >>> s3={1,2,3,4}
>>> s4={6,7,8,9}
>>> s3.union(s4)
{1, 2, 3, 4, 6, 7, 8, 9}

update

与另一个集合求并集,并返回给自身
Update a set with the union of itself and others.
>>> s3={1,2,3,4}
>>> s4={6,7,8,9}
>>> s3.update(s4)
>>> print(s3)
{1, 2, 3, 4, 6, 7, 8, 9}

认识python中的set集合及其用法的更多相关文章

  1. Python中字典和集合的用法

    本人开始学习python 希望能够慢慢的记录下去 写下来只是为了害怕自己忘记. python中的字典和其他语言一样 也是key-value的形式  利用空间换时间 可以进行快速的查找 key 是唯一的 ...

  2. Python中zip()与zip(*)的用法

    目录 Python中zip()与zip(*)的用法 zip() 知识点来自leetcode最长公共前缀 Python中zip()与zip(*)的用法 可以看成是zip()为压缩,zip(*)是解压 z ...

  3. python中的随机函数random的用法示例

    python中的随机函数random的用法示例 一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据 ...

  4. 简单说明Python中的装饰器的用法

    简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下   装饰器对与 ...

  5. Python中字典和集合

    Python中字典和集合 映射类型: 表示一个任意对象的集合,且可以通过另一个几乎是任意键值的集合进行索引 与序列不同,映射是无序的,通过键进行索引 任何不可变对象都可用作字典的键,如字符串.数字.元 ...

  6. Python中【__all__】的用法

    Python中[__all__]的用法 转:http://python-china.org/t/725 用 __all__ 暴露接口 Python 可以在模块级别暴露接口: __all__ = [&q ...

  7. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  8. Python中try...except...else的用法

    Python中try...except...else的用法: try:    <语句>except <name>:    <语句>          #如果在try ...

  9. Python中logging模块的基本用法

    在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...

随机推荐

  1. 仿腾讯课堂固定滚动列表ReactNative组件

    前言 由于业务需要做成类似腾讯课堂课程详情滚动的效果,考虑到后面有可能有新的呈现方式,RN提供的组件没有这种滚动控件,不如自己封装,其实去年已经写了一篇但是写的比较乱,周末花了点时间重写梳理下做的东西 ...

  2. vijos 1942 [AH 2005] 小岛

    描述 西伯利亚北部的寒地,坐落着由 N 个小岛组成的岛屿群,我们把这些小岛依次编号为 1 到 N . 起初,岛屿之间没有任何的航线.后来随着交通的发展,逐渐出现了一些连通两座小岛的航线.例如增加一条在 ...

  3. bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区

    Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...

  4. [51nod1502]苹果曼和纸

    苹果曼有很大的一张纸.这张纸的形状是1×n的长方形.你的任务是帮助苹果曼来折叠这一张纸.有一些操作,这些操作有如下两个种形式: 1. 把这张纸在第pi个位置对折.经过对折后,左边的1×pi部分会盖到右 ...

  5. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)

    以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...

  6. Coins、Tokens、山寨币:区别在哪里

    [译] Coins, Tokens & Altcoins: What's the Difference? coin:指"正宗的"数字货币,比如比特币 tokens:比如用 ...

  7. win处navicat直接导出的sql脚本导入Linux mysql报错问题

    最近几天在把win上的项目的数据库转移到Ubuntu,于是第一件事就是从win处的navicat直接导出sql脚本,然后进入Ubuntu导入的时候会报错误,跳过错误继续执行导致数据库表的缺失. 跨平台 ...

  8. 一个简洁的PHP可逆加密函数(分享)

    http://www.jb51.net/article/38018.htm 本篇文章是对一个简洁的PHP可逆加密函数进行了详细的分析介绍,需要的朋友参考下   很多时候我们需要对数据进行加密解密,比如 ...

  9. 怎样实现给DEDE的栏目增加栏目图片(2)

    2.3 打开dede/templets/catalog_edit.htm页面,查找 栏目名称:

  10. Hive 多分隔符的使用 (转载)

    方法一)通过org.apache.hadoop.hive.contrib.serde2.RegexSerDe格式的serde. 1) 建表语句 #指定以^|~作为分隔符 CREATE TABlE ta ...