基本数据类型-集合(set)_上周内容回顾(字符串_数字_列表_元组_字典_集合)
上周内容回顾
1、字符串

2、数字
除了布尔类型外,int、long、float和complex都可以使用的运算为:加、减、乘、除、整除、幂运算和取余
3、列表和元组
列表的内容可变,可以包含任意对象,使用中括号表示。
元组的内容不可变,可以包含任意对象,使用圆括号表示。元组
l = [1, 2, 3, '', ''] # 列表
l = list((1, 2, 3, '', '')) t = (1, 2, 3, '', '') # 元组
t = tuple("")

>>> l = [1, 2, 3, 4, 5]
>>> del l[1]
>>> l
[1, 3, 4, 5]
4、字典
字典(dict)是python为唯一的内置映射类型,任何不可变对象都可以用作字典的键值,如字符串、数字、元组等。字典使用大括号表示,键和值之间用冒号分割,各个键值间用逗号隔开。映射对象是无序的。
d = dict((['name', 'wuyuan'], ['age', 23]))
d = {'name': 'wuyuan', 'blog': 'wuyuans.com', 'age': 23}
d['school'] = 'HDU' # 添加一项

字典的历遍方式
#使用键历遍
for i in d:
print i,d[i]
#使用键值历遍
for k,v in d.items():
print k,v
5、可变不可变:
1、可变:列表,字典
2、不可变: 字符串,数字,元组
6、访问顺序:
1、直接访问:数字
2、顺序访问:字符串,列表,元组
3、映射: 字典
7、存放元素个数:
容器类型:列表,元组,字典
原子:数字,字符串
详细参考:

一、set集合(可变集合) 集合是一个无序而且不重复的集合,有些类似于数学中的集合,也可以求交集,求并集等
集合特性:
1、不同元素组成
2、无序
3、集合中元素必须是不可变类型
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object Build an unordered collection of unique elements.
"""
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 def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. 清除内容"""
pass def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. 浅拷贝 """
pass def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set. A中存在,B中不存在 (i.e. all elements that are in this set but not the others.)
"""
pass def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
pass 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 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 def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. 取交集并更更新到A中 """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
pass def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. 是否是子序列"""
pass def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. 是否是父序列"""
pass def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty. 移除元素
"""
pass 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 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 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
pass 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 def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. 更新 """
pass

set用法练习
练习一:
1、set输出
s=set('hello')
print(s)
执行结果:
{'l', 'e', 'h', 'o'}
2、set去重
ps1:
s=set(['alex','alex','sb'])
print(s)
执行结果:
{'sb', 'alex'}
ps2:
names=['alex','alex','wupeiqi']
names=list(set(names))
print(names)
执行结果:
['alex', 'wupeiqi']
3、add 添加元素 (只能更新一个值)
s = {1, 2, 3, 4, 5, 6}
s.add('s')
s.add('')
s.add(3)
print(s)
执行结果:
{1, 2, 3, 4, 5, 6, 's', ''}
4、clear 清空元素
s = {1, 2, 3, 4, 5, 6}
s.clear()
print(s)
执行结果:
set()
5、copy 拷贝元素
s = {1, 2, 3, 4, 5, 6}
s1=s.copy()
print(s)
执行结果:
{1, 2, 3, 4, 5, 6}
6、pop随机删
s={'sb',1,2,3,4,5,6}
s.pop()
print(s)
执行结果:
{2, 3, 4, 5, 6, 'sb'}
7、remove discard 指定删除
remove 删除不存在元素,会报错。
discard 删除不存在元素,不会报错。
s={'sb',1,2,3,4,5,6}
s.remove('sb')
s.remove('hellol') '''集合中不存在这个元素,删除不存在元素会报错'''
s.discard('sbbbb') '''集合中不存在这个元素,删除不存在元素不会报错'''
print(s)
执行结果:
1 File "D:/python/day5/集合.py", line 32
2 s.remove('hellol') '''删除元素不存在会报错'''
3 ^
4 SyntaxError: invalid syntax
二、集合的交集、并集、差集

集合支持一系列标准操作,包括并集、交集、差集和对称差集。
例如:
a = t | s # t 和 s的并集 b = t & s # t 和 s的交集 c = t – s # 求差集(项在t中,但不在s中) d = t ^ s # 对称差集(项在t或s中,但不会同时出现在二者中)
用法示例:
1、intersection 求交集(交集:两边都存在相同的内容,就取出来)
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s) #求交集 (交集:两边都存在相同的内容,就取出来)
print(p_s.intersection(l_s))
print(p_s&l_s)
执行结果:
{'szw', 'zjw', 'lcg'} {'szw', 'sb', 'lcg'}
{'szw', 'lcg'}
{'szw', 'lcg'}
2、union 求并集(并集:两个集合的元素合到一起)
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
print(p_s.union(l_s))
print(p_s|l_s)
执行结果:
{'lcg', 'zjw', 'szw'} {'lcg', 'sb', 'szw'}
{'lcg', 'szw', 'sb', 'zjw'}
{'lcg', 'szw', 'sb', 'zjw'}
3、difference 差集 (差集:两个数相减,得出的结果就是差集)
ps1:
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s) print('差集',p_s-l_s)
print(p_s.difference(l_s)) print('差集',l_s-p_s)
print(l_s.difference(p_s))
执行结果:
{'zjw', 'szw', 'lcg'} {'sb', 'szw', 'lcg'}
差集 {'zjw'} #左边减右边,减去左边和右边相同的,得出左边不相同的
{'zjw'}
差集 {'sb'} #右边减左边,减去右边和左边相同的,得出右边不相同的
{'sb'}
ps2: 差集 p_s-l_s
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
print('差集',p_s-l_s) #去掉同时报两门课程的人,得出只学一门课程的人
执行结果:
{'lcg', 'szw', 'zjw'} {'lcg', 'szw', 'sb'}
差集 {'zjw'} #结果得出只学一门课程的人
4、symmetric_difference 交叉补集 (交叉补集:先把两个合在一起,然后去掉两个都有的共同部分)

python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
print('交叉补集',p_s.symmetric_difference(l_s))
print('交叉补集',p_s^l_s)
执行结果:
{'szw', 'zjw', 'lcg'} {'szw', 'sb', 'lcg'}
交叉补集 {'sb', 'zjw'}
交叉补集 {'sb', 'zjw'}
5、差集 difference_update
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
# print('差集',p_s-l_s) #去掉同时报两门课程的人,得出只学一门课程的人
7 p_s=p_s-l_s
8 p_s=p_s-l_s = p_s.difference_update(l_s)
print(p_s)
6、isdisjoint 交集 (是否有交集,有的话返回False,没有的话返回True)
s1={1,2}
s2={3,5 #没有相同的就返回True
#s2={2,3,5} #有相同的就返回False
print(s1.isdisjoint(s2))
执行结果:
True #s2={3.5}
Flase #s3={2,3,5}
7、issubset (s1<=s2) s1是s2的子集 or s2是s1的父集
issuperset (s1>=s2) 判断一个集合是另一个集合的子集 (s2 是s1 的父集)

s1={1,2}
s2={1,2,3}
print(s1.issubset(s2)) #s1 是s2 的子集
print(s2.issubset(s1)) #False
print(s2.issuperset(s1)) #判断一个集合是另一个集合的子集 (s2 是s1的父集)
执行结果:
True
False
True
8、update 更新多个值
s1={1,2}
s2={1,2,3}
# s1.update(s2) #更新多个值
执行结果:
{1, 2, 3}
9、frozenset 不可变集合 元素一经创建,不可增加、删除和修改。因此没有add、pop、discard、remove和所有以_update结尾的方法。但可以作为左值接受赋值。
frozenset和set混合运算时,返回的值以左边的操作变量为准。
ps1:
frozenset(a) | set(b) 的返回值就是frozenset, set(a) | frozenset(b) 的返回值就是set
ps2:
s1=frozenset('hello')
print(s1)
执行结果:
frozenset({'l', 'h', 'o', 'e'})
其它练习
s1={1,2,3,1} #定义一个set s1 如果s1={}为空则默认定义一个字典
s2=set([2,5,6]) #定义一个set s2
print(s1) #s1={1,2,3} 自动去除重复的元素
s1.add(5) #s1={1,2,3,5} 添加一个元素
print(s1)
s3=s1.difference(s2) #返回一个s1中存在而不存在于s2的字典s3,s3={1,3},而s1并没有改变
print(s3)
s1.difference_update(s2) #s1跟新成上面的s3 s1={1,3}
s1.discard(1) #删除元素1,不存在的话不报错 s1={3}
print(s1)
s1.remove(3) #删除元素3,不存在的话报错 s1={}
print(s1)
s1.update([11,2,3]) #跟新s1中的元素,其实是添加 s1={11,2,3}
print(s1)
k=s1.pop() #删除一个元素,并将删除的元素返回给一个变量,无序的,所以并不知道删除谁
s1={1,2,3,4} #这里重新定义了集合s1,s2
s2={3,4,5,6}
r1=s1.intersection(s2) #取交集,并将结果返回给一个新的集合 r1={3,4}
print(r1)
print(s1)
s1.intersection_update(s2) #取交集,并将s1更新为取交集后的结果 s1={3,4}
print(s1)
k1=s1.issubset(s2) #s1是否是s2的的子序列是的话返回True,否则False 这里k1=true
print(k1)
k2=s1.issuperset(s2) #s1是否是s2的父序列 k2=False
k3=s2.isdisjoint(s1) #s1,s2,是否有交集,有的话返回False,没有的话返回True
print(k3)
s1.update([1,2]) #s1={1,2,3,4}
r3=s1.union(s2) #取并集将结果返回给r3 r3={1,2,3,4,5,6}
print(r3)
r2=s1.symmetric_difference(s2) #r2=s1并s2-s1交s2 r2={1,2,5,6}
print(r2)
s1.symmetric_difference_update(s2) #s1更新为 s1并s2 - s1交s2 s1={1,2,5,6}
print(s1)
基本数据类型-集合(set)_上周内容回顾(字符串_数字_列表_元组_字典_集合)的更多相关文章
- Python基础数据类型(数字、字符串、布尔、列表、元组、字典、集合等)
数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定 ...
- python基础知识3——基本的数据类型2——列表,元组,字典,集合
磨人的小妖精们啊!终于可以归置下自己的大脑啦,在这里我要把--整型,长整型,浮点型,字符串,列表,元组,字典,集合,这几个知识点特别多的东西,统一的捯饬捯饬,不然一直脑袋里面乱乱的. 一.列表 1.列 ...
- Python中列表、元组、字典、集合与字符串,相关函数,持续更新中……
本篇博客为博主第一次学 Python 所做的笔记(希望读者能够少点浮躁,认真阅读,平心静气学习!) 补充: 列表.元组和字符串共同属性: 属于有序序列,其中的元素有严格的先后顺序 都支持双向索引,索引 ...
- Python黑帽编程2.3 字符串、列表、元组、字典和集合
Python黑帽编程2.3 字符串.列表.元组.字典和集合 本节要介绍的是Python里面常用的几种数据结构.通常情况下,声明一个变量只保存一个值是远远不够的,我们需要将一组或多组数据进行存储.查询 ...
- Python中列表,元组,字典,集合的区别
参考文档https://blog.csdn.net/Yeoman92/article/details/56289287 理解Python中列表,元组,字典,集合的区别 列表,元组,字典,集合的区别是p ...
- Python 学习笔记(1)Python容器:列表、元组、字典与集合
Python容器:列表.元组.字典与集合 列表: 1.列表 的创建 使用[ ] 或者 list()创建列表:empty_list = [ ] 或者 empty_list= list() 使用list( ...
- Python入门系列(四)别再傻傻分不清:列表、元组、字典、集合的区别
总结分析列表.元组.字典.集合的相同与区别之处,只有彻底分清之后,就会在应用的时候,得心应手. 四句话总结 列表是一个有序且可更改的集合,允许重复成员. 元组是一个有序且不可更改的集合,允许重复成员. ...
- 菜鸟学IT之python3关于列表,元组,字典,集合浅认识!
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753 一.列表,元组,字典,集合分别如何增删改查及遍历. 列表 # 列表的 ...
- python_列表、元组、字典、集合对比
列表.元组.字典.集合 列表.元组.字典.集合对比 比较项 列表 元组 字典 集合 类型名称 list tuple dict set 定界符 [] () {} {} 是否可变 是 否 是 是 是否有序 ...
- python列表,元组,字典,集合的比较总结
这四个都是python中的序列,用于存放数据,他们区别总结如下: 列表list 元组tuple 字典dictionary 集合set 是否可变 可变 不可变 可变 可变 是否有序 有序 有序 无序 ...
随机推荐
- linux 文件管理以及其相关指令
Linux简介 严格的来讲,Linux 不算是一个操作系统,只是一个 Linux 系统中的内核, 即计算机软件与硬件通讯之间的平台:Linux的全称是GNU/Linux,这才算是一个真正意义上的Lin ...
- (转)linux下vi命令大全
http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n ...
- Eclipse 各种小图标的含义
Eclipse的Package Explorer和Outline中用图标表示了很多内容,其实能看懂这些图标在编程中也是很有用的. 绿色的小圆表示公有类型:如果是实心的就是函数(),如果是圈就表示是变量 ...
- 【原】webapp开发中兼容Android4.0以下版本的css hack
话说现在的手机型号越来越多,主要还是android和ios这2个巨头称霸了江湖,而他们自带的浏览器内核是webkit,那对于做移动网页开发的同事来说,一般只要做好webkit内核浏览器的展现效果就行了 ...
- AC日记——找最大数序列 openjudge 1.9 10
10:找最大数序列 总时间限制: 1000ms 内存限制: 65536kB 描述 输入n行,每行不超过100个无符号整数,无符号数不超过4位.请输出最大整数以及最大整数所在的行号(行号从1开始). ...
- 升级Flash Builder 4.6中的Flash Player版本
测试有效 本人按此方法升级到了flash player 15 Adobe自发布Flash Builder 4.6后,就暂停了Flash Builder新版本的发布.但AIR和FlashPlayer版本 ...
- url编码base编码解码十六进制
0x25346425353425343525333525343325366125343525373725346425353125366625373825346425343425363725346225 ...
- LinkedList子类与Queue接口
LinkedList表示的是一个链表的操作类.定义如下: public class LinkedList<E> extends AbstractSequentialList<E> ...
- Mysql备份系列(1)--备份方案总结性梳理
mysql数据库备份有多么重要已不需过多赘述了,废话不多说!以下总结了mysql数据库的几种备份方案: 一.binlog二进制日志通常作为备份的重要资源,所以再说备份方案之前先总结一下binlog日志 ...
- Location of several networks in brain
Source: Naci, L., et al. (2014). "A common neural code for similar conscious experiences in dif ...