1、添加一个元素:
    add(...)

Addan element to a set.

1
2
3
4
>>> a = {'shaw',11,22}
>>>a.add('sina')
>>> a
{'sina', 11, 22,'shaw'}

2、清空集合

clear(...)

Remove all elements from this set.

1
2
3
4
>>> a = {'shaw',11,22}
>>>a.clear()
>>> a
set()

3、浅copy

copy(...)

Return a shallow copy of a set.

1
2
3
4
>>> a = {'shaw',11,22}
>>> b =a.copy()
>>>print(b)
{11, 22, 'shaw'}

4、返回一个A中存在,B中不存在元素的集合

difference(...)

Return the difference of two or more setsas a new set.

1
2
3
4
>>> A = {'shaw',11,22}
>>> B ={11,"sam"}
>>>A.difference(B)
{22, 'shaw'}

5、从当前元素中删除和B中相同的元素

difference_update(...)

Remove all elements of another set fromthis set.

1
2
3
4
5
>>> A = {'shaw',11,22}
>>> B ={11,"sam"}
>>>A.difference_update(B)
>>>print(A)
{22, 'shaw'}

6、删除指定元素,如果不存在不会报错

discard(...)

Remove an element from a set if it is amember.

1
2
3
4
5
6
7
>>> A = {'shaw',11,22}
>>>A.discard(11)
>>>print(A)
{22, 'shaw'}
>>>A.discard(15)
>>>print(A)
{22, 'shaw'}

7、取交集

intersection(...)

Return the intersection of two sets as anew set.

1
2
3
4
>>> a = {'shaw',11,22}
>>> b ={'sam',22}
>>>a.intersection(b)
{22}

8、取交集,并更新到A中

intersection_update(...)

Update a set with the intersection ofitself and another.

1
2
3
4
5
>>> A = {'shaw',11,22}
>>> B ={'sam',22}
>>>A.intersection_update(B)
>>> print(A)
{22}

9、判断是否有交集,如果没有交集,返回True,否则返回False

isdisjoint(...)

Return True if two sets have a nullintersection.

1
2
3
4
>>> A = {'shaw',11,22}
>>> B ={'sam',22}
>>>A.isdisjoint(B)
False

10、判断是否是子序列

issubset(...)

Report whether another set contains thisset.

1
2
3
4
5
6
7
>>> A = {'shaw',11,22}
>>> B ={'sam',22}
>>>B.issubset(A)
False
>>> C ={'shaw'}
>>>C.issubset(A)
True

11、判断是否是父序列

issuperset(...)

Report whether this set contains anotherset.

1
2
3
4
>>> A = {'shaw',11,22}
>>> B ={22}
>>>A.issuperset(B)
True

12、移除元素,如果集合为空,会报错

pop(...)

Remove and return an arbitrary setelement.

Raises KeyError if the set is empty.

1
2
3
4
5
>>> A = {'shaw',11,1,98,3,'abc','Shaw'}
>>>A.pop()
1
>>>print(A)
{98, 3, 'abc', 11,'Shaw', 'shaw'}

13、删除指定元素,元素不存在会报错

remove(...)

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

If the element is not a member, raise aKeyError.

1
2
3
4
5
6
7
8
>>> A = {'shaw',11,22,1,98,3,'abc','Shaw'}
>>>A.remove('22')
Traceback (mostrecent call last):
   File "<input>", line 1, in<module>
KeyError: '22'
>>>A.remove(22)
>>>print(A)
{1, 98, 3, 'abc',11, 'Shaw', 'shaw'}

14、取并集

union(...)

Return the union of sets as a new set.

1
2
3
4
>>> A = {'shaw',11,22,1,98,3,'abc','Shaw'}
>>> B ={11,'sam'}
>>>A.union(B)
{'sam', 1, 98, 3,'abc', 11, 'Shaw', 22, 'shaw'}

15、对称差集

symmetric_difference(...)

Return the symmetric difference of twosets as a new set.

1
2
3
4
>>> A = {'shaw',11,22,1,98,3,'abc','Shaw'}
>>> B ={11,'sam'}
>>>A.symmetric_difference(B)
{'sam', 1, 98, 3,'abc', 'Shaw', 22, 'shaw'}

16、更新

update(...)

Update a set with the union of itself andothers.

1
2
3
4
5
>>> A = {'shaw',11,22,1,98,3,'abc','Shaw'}
>>> B ={11,'sam'}
>>>A.update(B)
>>>print(A)
{'sam', 1, 98, 3,'abc', 11, 'Shaw', 22, 'shaw'}

Python 集合方法总结的更多相关文章

  1. Python集合方法整理(Day9)

    #作用:去重,关系运算, #定义: 知识点回顾 可变类型是不可hash类型 不可变类型是可hash类型 #定义集合: 集合:可以包含多个元素,用逗号分割, 集合的元素遵循三个原则: 1:每个元素必须是 ...

  2. Python 集合set添加删除、交集、并集、集合操作符号

    在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...

  3. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

  4. [转]python集合set

    Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 来源网 ...

  5. python集合set,frozenset--笔记

    <Python3程序开发指南>笔记. python提供了2种内置的集合类型:可变的set类型.固定的frozenset类型. 只有可哈希运算的对象可添加到集合中.可哈希的数据类型:floa ...

  6. Python魔法方法总结及注意事项

    1.何为魔法方法: Python中,一定要区分开函数和方法的含义: 1.函数:类外部定义的,跟类没有直接关系的:形式: def func(*argv): 2.方法:class内部定义的函数(对象的方法 ...

  7. Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了

    Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: ...

  8. python 去重方法

    待补充:https://www.cnblogs.com/zknublx/p/6042295.html 一.使用集合直接去重 ids = [1,4,3,3,4,2,3,4,5,6,1]ids = lis ...

  9. #8 Python数学方法

    前言 前几节了解了Python的不同数据类型,有小伙伴会问,不同的数据类型之间是否可以相互转换?肯定是可以的,本篇博文主要记录数字类型的转换,其他类型的相互转换会在下几节记录,Here we go! ...

随机推荐

  1. spring AOP 实现事务和主从读写分离

    1 切面 是个类 2 切入点 3 连接点 4 通知 是个方法 5 配置文件 <?xml version="1.0" encoding="UTF-8"?&g ...

  2. Qt工程使用第三方库——Qt下使用glut库

    本人使用的环境 操作系统:windows10 Qt构建套件:qt-mingw4.8.5 + mingw4.4.0 Qt Creator版本:3.6.1   本教程配置针对工程而言,每个工程需要单独配置 ...

  3. crontab不能正确执行的问题

    近期在部署crontab任务的时候,总是遇到在shell中单独执行正常,但是放到crontab定时执行出错的问题.若出现这类场景,九成就是环境变量的问题. 因为我的定制任务,基本上都需要使用sqlpl ...

  4. 基于redis的排行榜设计和实现

    前言: 最近想实现一个网页闯关游戏的排行榜设计, 相对而言需求比较简单. 秉承前厂长的训导: “做一件事之前, 先看看别人是怎么做的”. 于是乎网上搜索并参考了不少排行榜的实现机制, 很多人都推荐了r ...

  5. oracle 常用语法

    一.ORACLE的启动和关闭1.在单机环境下要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下su - oraclea.启动ORACLE系统oracle>svrmgrlSVRMG ...

  6. Array数组

    array_diff_key() 例子 , , 'purple' , , 'yellow' ); var_dump(array_diff_key($array1, $array2));?> 上例 ...

  7. mysql 把文件中的sql语句导入到mysql中

    mysql -uroot -proot -Dcollege</home/wwwroot/default/data/xlxxb_2014-10-16.txt;

  8. JavaBean基本用法示例(一)

    一.首先创建一个类person,里面有四个成员:name,sex,age,info,添加四个成员所有的set和get方法. package com.kaly.bean; public class pe ...

  9. 士兵站队问题sol

    作者:http://www.cnblogs.com/taoziwel/articles/1859577.html 相类似题目:输油管道问题 [问题描述] 在一个划分成网格的操场上,n个士兵散乱地站在网 ...

  10. 黄聪:说说JSON和JSONP,也许你会豁然开朗(转)

    前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...