1 概念

  集合是一个无需的,不重复的数组组合,它的主要作用如下:

    去重,将一个列表装换成集合,会将其去重

    关系测试,测试两组数据的交集,差集,并集等关系。

  集合对象是一组无需排列的可哈希的值,集合成员可以作为字典的键。

  集合中的元素不可以是列表或字典

2 集合的创建

 s1 = set('hello word')
print(s1)
# {'o', 'e', 'h', 'w', 'd', 'l', ' ', 'r'} s2 = set(['hello', 'word'])
print(s2)
# {'word', 'hello'}

3 集合的访问

  由于集合是无序的,所以不能为集合创建索引或切片操作,只能循环遍历或使用in 、not in来访问判断集合元素

 s = set('hello word')
print('h' in s) # True
print('a' in s) # False
for i in s:
print(i)
# l
# d
# w
# h
# r
# e
#
# o

4 集合的更新

  可以用以下方法来更新集合

  s.add()

  s.update()

  s.remove()

  del: 可以删除集合本身

 # 集合的更新
s = set('hello word')
s.add('mm')
print(s) # {'r', 'd', 'w', 'l', 'mm', 'h', 'o', ' ', 'e'} s.update('AB')
print(s) # {'h', 'o', 'B', 'd', 'e', 'mm', 'r', ' ', 'l', 'w', 'A'} s.remove('h')
print(s) # {'B', 'mm', 'd', 'e', 'o', 'A', ' ', 'w', 'r', 'l'}

  注意:

 s = set([1, 2, 'word'])
s.update([12, 'aaa'])
print(s) # {1, 2, 12, 'word', 'aaa'}

5 集合类型操作符

  1 等价与不等价(==, !=)

 s1 = set('hello')
s2 = set('hellollo')
print(s1 == s2) # True

  2 子集,超集

 s1 = set('hello word')
s2 = set('hello')
print('h' in s1) # True
print(s2 < s1) # True

  注意

 print(set('hello') < set('hellollo'))  # False

  3 并集(|)

    联合(union)操作与集合的or操作其实是等价的,联合符号有个等价的方法 union()

 s1 = set('hello')
s2 = set('word')
s3 = s1 | s2
print(s3) # {'w', 'o', 'r', 'e', 'd', 'l', 'h'}
s4 = s1.union(s2)
print(s4) # {'w', 'o', 'r', 'e', 'd', 'l', 'h'}

  4 交集(&)

    与集合and等价,交集负号的等价方法是intersection()

 # 交集
s1 = set('hello')
s2 = set('word')
s3 = s1 & s2
print(s3) # {'o'}
s4 = s1.intersection(s2)
print(s4) # {'o'}

  5 差集(-)

    等价方法是difference()

 # 差集(-)
s1 = set('hello')
s2 = set('word')
s3 = s1 - s2
print(s3) # {'e', 'h', 'l'}
s4 = s1.difference(s2)
print(s4) # {'e', 'h', 'l'}

  6 对称差集(^)

    对称差集是集合的异或, 取得元素属于原来两个集合,但是不同时属于原来的两个集合,其等价方法是symmetric_difference()

 # 对称差集
s1 = set('hello')
s2 = set('world')
s3 = s1 ^ s2
print(s3) # {'r', 'h', 'w', 'e', 'd'}
s4 = s1.symmetric_difference(s2)
print(s4) # {'r', 'h', 'w', 'e', 'd'}

python基础语法之集合set的更多相关文章

  1. python基础语法、数据结构、字符编码、文件处理 练习题

    考试范围 '''1.python入门:编程语言相关概念2.python基础语法:变量.运算符.流程控制3.数据结构:数字.字符串.列表.元组.字典.集合4.字符编码5.文件处理''' 考试内容 1.简 ...

  2. python基础语法及知识点总结

    本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...

  3. Python基础语法题库

    引言: 语法练习包括Python基础语法.数据类型.字符编码和简单文件操作等内容. 正文(参考答案附录在题目下方): 1.Python 里用来告知解释器跳过当前循环中的剩余语句,然后继续进行下一轮循环 ...

  4. 尚学python课程---13、python基础语法

    尚学python课程---13.python基础语法 一.总结 一句话总结: legend2系统使我能够快速掌握一门语法,特别有用 pass 语句:空语句:是为了保持程序结构的完整性  :作用:比如: ...

  5. python基础语法(变量与数据类型)

    python基础语法(变量与数据类型) 一.python变量 python中的变量不需要声明.每个变量在使用钱都需要赋值,变量赋值以后,该变量才会被创建 在python中,变量就是变量,它没有类型,我 ...

  6. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  7. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  8. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  9. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

随机推荐

  1. 【leetcode】1244. Design A Leaderboard

    题目如下: Design a Leaderboard class, which has 3 functions: addScore(playerId, score): Update the leade ...

  2. Jquery实现div左右重复来回走动

    <!DOCTYPE HTML><html><head> <meta charset=utf-8 /> <title>UFO来了</ti ...

  3. Vue-CLI项目搭建

    一.环境搭建 1.安装服务器node 官网下载 https://nodejs.org/zh-cn/ node:用C++语言编写,用来运行JavaScript语言 node可以为前端项目提供server ...

  4. POJ 6621: K-th Closest Distance(主席树 + 二分)

    K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Jav ...

  5. R_Studio(癌症)以等宽类别值、自定义类别值、等频类别值(分为5类)

    对“癌症.csv”中的肾细胞癌组织内微血管数进行连续属性的离散化处理 增加“微血管数分类1”属性,取值为等宽类别值(分为5类),增加“微血管数分类2”属性,取值为自定义类别值(0~40,41~60,6 ...

  6. phoenix-hbase 使用

    建表命令 CREATE TABLE IF NOT EXISTS "person_mul"( "ROW" varchar primary key, //主键,必须 ...

  7. 项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)

    这次在配置xml文件时,出现错误提示( The reference to entity “useSSL” must end with the ‘;’ delimiter.) 报错行为 <prop ...

  8. Java多线程核心知识

    多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线程至关 ...

  9. echarts_04

    通过ajax加载本地文件: 1.http://www.cnblogs.com/qjqcs/archive/2016/09/07/5850282.html 2.http://blog.csdn.net/ ...

  10. SpringBoot整合Mybatis,并实现事务控制

    SpringBoot整合Mybatis,并实现事务控制 1. 在pom文件里添加相关maven文件 <parent> <groupId>org.springframework. ...