字典常用的就是,他的去重。

set集合是python的一个基本数据类型.
set中的元素是不重复的.⽆无序的.⾥面的元素必须是可hash的(int, str, tuple,bool)。
我们可以这样来记. set就是dict类型的数据但是不保存value, 只保存key. set也⽤{}表⽰
注意:
set中的元素是不重复的, 且无序的.
使⽤用这个特性.我们可以使⽤用set来去掉重复
set集合中的元素必须是可hash的, 但是set本身是不可hash得. set是可变的。 set集合增删改查 1.增加
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 用法:添加一个元素到集合。重复的内容不会被添加到set集合。 例子:
s = {"刘大哥", '关大哥', "张大哥"}
s.add("赵子龙")
print(s)
s.add("赵子龙") # 重复的内容不不会被添加到set集合中
print(s) def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass 用法:迭代更新 例子:
s = {"刘大哥", '关大哥', "张大哥"}
s.update("赵子龙") # 迭代更更新
print(s)
s.update(["阿斗", "卧龙","凤雏"])
print(s) 2.删除
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass 用法:随机删除一个元素,如果集合为空,会报错。 例子: s = {"刘大哥", '关大哥', "王大哥","张哥哥"}
item = s.pop() # 随机弹出⼀一个.
print(s)
print(item) 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 用法:删除集合中指定成员,如果成员不存在会报错。 例子:
s = {"刘大哥", '关大哥', "王大哥","张哥哥"}
s.remove("关大哥") # 直接删除元素
# s.remove("⻢马") # 不不存在这个元素. 删除会报错
print(s) 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 用法:删除集合中指定成员,如果该集合不存在所删除成员,不做任何操作,也不会报错。 例子:
s = {1, 3, 4, 5}
print(s)#{1, 3, 4, 5}
s.discard(3)
print(s)#{1, 4, 5}
s.discard(66)
print(s)#{1, 4, 5} def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass clear() 清空set集合。
注意:set集合如果是空的。打印出来的是set()。需要和dict区分。
例子:
s = {1, 3, 4, 5}
s.clear()
print(s)。#set() 3.修改
set 集合中的数据没有索引,也没有办法定位一个元素。所以没有办法直接修改。
我们可以采用:
1)先删除后添加的方式
2)将set转为list删除元素后将list转为set 4.查询
set是一个可迭代对象,所以可以进行for循环
for x in s:
print(x) 5.常用操作 1.求差集(-)
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 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1-s2)#{9, 5, 7}
print(s1.difference(s2))#{9, 5, 7}
print(s2-s1)#{8, 10, 6}
print(s2.difference(s1))#{8, 10, 6} 2.求交集(&)
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 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1&s2)#{1, 3}
print(s1.intersection(s2))
print(s2&s1)#{1, 3}
print(s2.intersection(s1)) 3.求对称差集(^)
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 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1 ^ s2)#{5, 6, 7, 8, 9, 10}
print(s1.symmetric_difference(s2))#{5, 6, 7, 8, 9, 10}
print(s2 ^ s1)#{5, 6, 7, 8, 9, 10}
print(s2.symmetric_difference(s1))#{5, 6, 7, 8, 9, 10} 4.求并集(|)
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 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1 | s2)#{1, 3, 5, 6, 7, 8, 9, 10}
print(s1.union(s2))#{1, 3, 5, 6, 7, 8, 9, 10}
print(s2 | s1)#{1, 3, 5, 6, 7, 8, 9, 10}
print(s2.union(s1))#{1, 3, 5, 6, 7, 8, 9, 10} 5.子集
def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass 例子:
s1 = {"1", "2"}
s2 = {"1", "2", "3"}
# ⼦子集
print(s1 < s2) # set1是set2的⼦子集吗? True
print(s1.issubset(s2)) 6.超集
def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. """
pass 例子:
s1 = {"1", "2"}
s2 = {"1", "2", "3"}
print(s1 > s2) # set1是set2的超集吗? False
print(s1.issuperset(s2))

Python集合的常用操作的更多相关文章

  1. 『无为则无心』Python序列 — 22、Python集合及其常用操作

    目录 1.Python集合特点 2.Python集合的创建 3.操作集合常用API (1)增加数据 @1.add()方法 @2.update()方法 (2)删除数据 @1.remove()方法 @2. ...

  2. [PY3]——内置数据结构(6)——集合及其常用操作

    集合及其常用操作Xmind图          集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...

  3. Python集合类型的操作与应用

    Python集合类型的操作与应用 一.Python集合类型 Python中的集合类型是一个包含0个或多个数据项的无序的.不重复的数据组合,其中,元素类型只能是固定数据类型,如整数.浮点数.字符串.元组 ...

  4. Redis集合的常用操作指令

    Redis集合的常用操作指令 Sets常用操作指令 SADD 将指定的元素添加到集合.如果集合中存在该元素,则忽略. 如果集合不存在,会先创建一个集合然后在添加元素. 127.0.0.1:6379&g ...

  5. 二叉树的python可视化和常用操作代码

    二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...

  6. Python数据类型及常用操作

    Python字符串类型 1.用途: 用来记录有描述性的状态.比如:人名,地址等. 2.定义方式: 创建字符串非常简单,在‘ ’,“ ”,‘’‘ ’‘’内一填写一系列的字符例如:msg='hello' ...

  7. Python字符串的常用操作学习

    >>> name = "I love my job!" >>> name.capitalize() #首字母大写 'I love my job! ...

  8. python os 模块常用操作

    python 2.7 os 常用操作 官方document链接 文件和目录 os.access(path, mode) 读写权限测试 应用: try: fp = open("myfile&q ...

  9. 初识python: 字符串常用操作

    直接上代码示例: #!/user/bin env python # author:Simple-Sir # time:20180914 # 字符串常用操作 name = 'lzh lyh' print ...

随机推荐

  1. elementaryos必装软件

    所使用版本:elementaryos-0.4-stable-amd64.20160909.iso vmtools jdk sougouinput IntellijIEAD

  2. html5--1.19 通用属性

    html5--1.19 通用属性 学习要点: 1.通用属性的概念及几个常用的通用属性2.对属性值的若干点补充 通用属性 通用属性(全局属性)可以用于任何的HTML5元素:通用属性有十几种:这节课不会全 ...

  3. sublime 3好用快捷键

    sublime 3好用快捷键 自己常用 删除行 [ { "keys": ["ctrl+shift+d"], "command": " ...

  4. 使用IE11的F12开发人员工具进行网页前端性能测试

    用IE访问被测网站(我的是IE11,EDGE浏览器相同),定位到你要测试的动作所在页面或被测页面的前一页.按F12调出开发人员工具,其它的功能我就不介绍了,直接切换到性能选项卡. 根据提示按快捷键ct ...

  5. Unity 官方自带的例子笔记 - Space Shooter

    首先 买过一本叫 Unity3D开发的书,开篇第一个例子就是大家经常碰见的打飞机的例子,写完后我觉得不好玩.后来买了一本 Unity 官方例子说明的书,第一个例子也是打飞机,但是写完后发现蛮酷的,首先 ...

  6. EVC入门之二: 在未被加载的DLL中设置断点 (虽然没有遇到这个问题,不过先摘抄下来)

    问题: 这个问题居然也郁闷了我一段时间. 我们假设在EVC里建立了一个project, 里面有SubProject_1(以下简称SB1,嘿嘿), 编译生成一个EXE; SubProject_2(以下简 ...

  7. OpenCV——PS 滤镜算法之极坐标变换到平面坐标

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  8. HDU 1166 敌兵布阵 (线段树单点修改和区间和查询)

    Input 第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1 ...

  9. ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

  10. TreeView滚动TreeViewItem

    今天帮忙修了一个bug, 在拖动TreeViewItem时,需要滚动TreeView向前翻页,或向后翻页. 思路: 1.找到TreeView控件里的ItemsControl 2.找到ItemsCont ...