数据类型:int/str/bool/list/dict/tuple/float/set   (set类型天生去重)

一、集合的定义

s = set()  #定义空集合

s = {'a','b','c','d'}   #集合不是key-value  形的,无冒号

集合是无序的,没办法通过下标取值

二、集合赋值

s.add()

s = {'a','b','c','d'}
s.add('ijk') #注意add 与 update 的区别
# s.update('fgh')
print(s)

输出结果:

{'d', 'ijk', 'c', 'b', 'a'}

s.update()

输出结果:

{'f', 'b', 'g', 'd', 'a', 'c', 'h'}

s = set()

s = set('cheeseshop')
print(s)

输出结果:

{'s', 'e', 'p', 'h', 'o', 'c'}

三、删除集合元素

s.remove()
s = set('cheeseshop')
s.remove('er') # 删除不存在的会报错
s.remove('e')
print(s)

s.pop()  #随机删除一个

s.discard('er')  #如果删除的元素存在,删除,不存在不做处理

del s  # 删除集合

四、集合常用操作

s -= set('copy')   等价于  s = s - set('copy')

取交集

s.intersection(s1)   等价于  s & s1

取并集

s.union(s1)   等价于  s | s1

取差集

s.difference(s1)   等价于  s - s1
取对称差集
s.symmetric_difference(s1)   等价于 s^s1   取既不存在于s ,也不存在于s1中的元素

示例如下:
s = set('hi')
t = set('hello')
print(s.symmetric_difference(t)) 输出结果:
{'e', 'i', 'l', 'o'}
 
												

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

  1. python列表字符串集合常用方法

    1.1 列表常用方法 # 1. append 用于在列表末尾追加新的对象a = [1,2,3]a.append(4) # the result : [1,2,3,4]​# 2. count方法统计某个 ...

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

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

  3. [转]python集合set

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

  4. Python 集合set()添加删除、交集、并集、集合操作详解

    集合:一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次.每个元素的地位都是相同的,元素之间是无序的. 创建集合set python set类是在python的sets模块中,大家现在使 ...

  5. python集合使用范例的代码

    在代码过程中中,将代码过程中比较好的代码段珍藏起来,如下的代码是关于python集合使用范例的代码,希望能对大伙有用. # sets are unordered collections of uniq ...

  6. python集合与字典的用法

    python集合与字典的用法 集合: 1.增加  add 2.删除   •del 删除集合 •discard(常用)删除集合中的元素  #删除一个不存在的元素不会报错 •remove 删除一个不存在的 ...

  7. Python 集合内置函数大全(非常全!)

    Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员   s ...

  8. python集合可以进行相减

    python集合可以进行相减 student = {'tom','jim','mary','tom','jack','rose'} print(student) print('rose' in stu ...

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

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

随机推荐

  1. 解决Centos运行yum 报错:坏的解释器

    # ll /usr/bin/python python python2 python2.6 python2.7 python.bak 1,这里先备份原来的/usr/bin/python 为python ...

  2. Flask参数解析、请求钩子

    转载请注明出处 https://www.cnblogs.com/chenxianpao/p/9949279.html  参数解析 Flask的参数解析主要用Request完成(from flask i ...

  3. [转]Understanding Integration Services Package Configurations

    本文转自:http://msdn.microsoft.com/en-us/library/cc895212.aspx Introduction With the 2008 release, SQL S ...

  4. ubuntu下cmake自动化编译的一个例子

    一个CMakeLists.txt的例子参考:https://www.hahack.com/codes/cmake/https://blog.csdn.net/afei__/article/detail ...

  5. http://blog.csdn.net/congcong68/article/details/39256307

    http://blog.csdn.net/congcong68/article/details/39256307

  6. 【Linux】shell判断mysql端口是否启用?

    #!/bin/bash set -eux mysqld >& & CONNECTED=$(netstat -alnt | grep -c ":3306 ") ...

  7. [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions

    We only have a few dispatching functions that need to be known by our React Application. Each one ac ...

  8. 向大家推荐两个灰常好用的插件LigerUI和报表控件highcharts

    废话不多说上一张图看看,向大家推荐两个灰常好用的插件LigerUI和报表控件highcharts.欢迎大家进技术群讨论:QQ群:15129679 http://ligerui.com/和http:// ...

  9. sublime HtmlPrettify

    用sublime都快一年多了,终于找到一款称心如意的format工具,可以同时格式化 html css js. HTML-CSS-JS Prettify 如果安装的时候出现问题,多调试调试,改改参数

  10. OkDownload项目实战

    本文介绍项目中引入okhttp-okgo开源框架里的OkDownload部分,实现了RecyclerView列表的下载功能. 引入OKDownload 需求不仅是要支持断点续传,而且还要支持队列下载和 ...