基本数据类型-集合(set)_上周内容回顾(字符串_数字_列表_元组_字典_集合)
2016-11-29 22:33
946 查看
上周内容回顾
1、字符串
2、数字
除了布尔类型外,int、long、float和complex都可以使用的运算为:加、减、乘、除、整除、幂运算和取余
3、列表和元组
列表的内容可变,可以包含任意对象,使用中括号表示。
元组的内容不可变,可以包含任意对象,使用圆括号表示。元组
4、字典
字典(dict)是python为唯一的内置映射类型,任何不可变对象都可以用作字典的键值,如字符串、数字、元组等。字典使用大括号表示,键和值之间用冒号分割,各个键值间用逗号隔开。映射对象是无序的。
字典的历遍方式
5、可变不可变:
1、可变:列表,字典
2、不可变: 字符串,数字,元组
6、访问顺序:
1、直接访问:数字
2、顺序访问:字符串,列表,元组
3、映射: 字典
7、存放元素个数:
容器类型:列表,元组,字典
原子:数字,字符串
详细参考:
一、set集合(可变集合) 集合是一个无序而且不重复的集合,有些类似于数学中的集合,也可以求交集,求并集等
集合特性:
1、不同元素组成
2、无序
3、集合中元素必须是不可变类型
set用法练习
练习一:
1、set输出
执行结果:
2、set去重
ps1:
执行结果:
ps2:
执行结果:
3、add 添加元素 (只能更新一个值)
执行结果:
4、clear 清空元素
执行结果:
5、copy 拷贝元素
执行结果:
6、pop随机删
执行结果:
7、remove discard 指定删除
remove 删除不存在元素,会报错。
discard 删除不存在元素,不会报错。
执行结果:
二、集合的交集、并集、差集
集合支持一系列标准操作,包括并集、交集、差集和对称差集。
例如:
用法示例:
1、intersection 求交集(交集:两边都存在相同的内容,就取出来)
执行结果:
2、union 求并集(并集:两个集合的元素合到一起)
执行结果:
3、difference 差集 (差集:两个数相减,得出的结果就是差集)
ps1:
执行结果:
ps2: 差集 p_s-l_s
执行结果:
4、symmetric_difference 交叉补集 (交叉补集:先把两个合在一起,然后去掉两个都有的共同部分)
执行结果:
5、差集 difference_update
6、isdisjoint 交集 (是否有交集,有的话返回False,没有的话返回True)
执行结果:
7、issubset (s1<=s2) s1是s2的子集 or s2是s1的父集
issuperset (s1>=s2) 判断一个集合是另一个集合的子集 (s2 是s1 的父集)
执行结果:
8、update 更新多个值
执行结果:
9、frozenset 不可变集合 元素一经创建,不可增加、删除和修改。因此没有add、pop、discard、remove和所有以_update结尾的方法。但可以作为左值接受赋值。
frozenset和set混合运算时,返回的值以左边的操作变量为准。
ps1:
ps2:
执行结果:
其它练习
1、字符串
2、数字
除了布尔类型外,int、long、float和complex都可以使用的运算为:加、减、乘、除、整除、幂运算和取余
3、列表和元组
列表的内容可变,可以包含任意对象,使用中括号表示。
元组的内容不可变,可以包含任意对象,使用圆括号表示。元组
l = [1, 2, 3, '4', '5'] # 列表 l = list((1, 2, 3, '4', '5')) t = (1, 2, 3, '4', '5') # 元组 t = tuple("1234")
>>> 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('3') s.add(3) print(s)
执行结果:
{1, 2, 3, 4, 5, 6, 's', '3'}
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)
相关文章推荐
- python实操(5):python数据类型及常用操作,数字、字符串、元组、列表、字典、集合
- Python之几种重要的基本类型:元组,列表,字典,字符串,集合
- python之一五大基本类型(数字、字符串、列表、元组、字典)
- 基本数据类型-列表_元组_字典_day4
- Python(5-6-7):数字+字符串+列表+元组+字典+集合
- Python 学习 (一) python的基本数据类型 元组 列表 字典
- Objective-C 的基本数据类型、数字、字符串和集合等介绍
- 字典(dict),列表(list),元组(tuple),集合(*set)的基本概述
- Python数据类型(元组、列表、字符串、字典)
- Objective-C 的基本数据类型、数字、字符串和集合等介绍 (retaincount 释放问题)
- Objective-C 的基本数据类型、数字、字符串和集合等介绍
- 列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 的基本方法
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- Python基础——原生数据类型(字典,列表,元组,字符串)
- 【python】特色数据类型——元组(列表、元组、字典、集合)(6)
- Python3组合数据类型(元组、列表、集合、字典)语法
- Python基础数据类型-列表(list)和元组(tuple)和集合(set)
- Python 3从入门到精通14-常见的数据类型字符串元组,列表,字典
- Python基础 -- 02 基础语法(字符串、列表、元组、字典、set集合)
- 【八】列表、字典、集合、元组间的数据类型转换总结