元组
元组的定义
Python内置的数据结构之一,是一个不可变序列。例如:t=(‘Python’,‘hello’,90)
不可变序列与可变序列:
不可变序列——不能进行增删改查的操作----字符串、元组
可变序列——可以执行增删改查操作,对象地址不发生改变----列表、字典
'''可变序列 列表, 字典''' lst = [10,20,45] print(id(lst)) # 1730043981184 lst.append(100) print(id(lst)) # 1730043981184 '''不可变序列 字符串,元组''' s = 'hello' print(id(s)) # 2003893431856 s = s+'world' print(id(s)) # 2003893785584 print(s) # helloworld
元组的创建方式
- 直接使用() ——小括号可以省略
- 使用内置函数 tuple()
- 元组中只有一个元素时,使用逗号和小括号,逗号不能省
'''第一种创建方式,使用()''' t = ('python','world',98) print(t) # ('python', 'world', 98) print(type(t)) #<class 'tuple'> t2 = 'python','world',98 print(t2) #('python', 'world', 98) print(type(t2)) #<class 'tuple'> t3 = ('python',) # 如果元组中只有一个元素,逗号不能省 print(t3) print(type(t3)) '''第二种创建方式,使用内置函数tuple()''' t1=tuple(('python','world',98)) print(t1) #('python', 'world', 98) print(type(t1)) #<class 'tuple'> '''空元组的创建方式''' lst=[] lst1=list() d={} d1=dict() t=() t1=tuple() print('空列表',lst,lst1) #空列表 [] [] print('空字典',d,d1) #空字典 {} {} print('空元组',t,t1) #空元组 () ()
元组不可变原因
为什么要将元组设计成不可变序列?
- 在多任务环境下,同时操作对象时不需要加锁,因此在程序中尽量使用不可变序列
注意事项:
元组中存储的是对象的引用
- 如果元组中对象不可变,则不能再引用其他对象,即不可增删元素;
- 如果元组中对象可变,则可变对象的引用不允许改变,但该可变对象中数据可以进行增删操作;
t = (10,[10,30],9) print(t,type(t),id(t)) #(10, [10, 30], 9) <class 'tuple'> 2422344883968 print(t[1],type(t[1]),id(t[1])) #[10, 30] <class 'list'> 2790961392960 t[1].append(100) # 列表可变,可以增加元素 print(t,type(t),id(t)) #(10, [10, 30, 100], 9) <class 'tuple'> 2422344883968 print(t[1],type(t[1]),id(t[1])) # [10, 30, 100] <class 'list'> 2790961392960
元组的遍历
元组是可迭代对象,所以可以使用for…in进行遍历
t = ('python','world',98) '''第一种获取元组中元组的方式,使用索引''' print(t[0]) print(t[1]) print(t[2]) print(t[3]) # 报错:IndexError: tuple index out of range '''第二种,遍历元组''' for item in t: print(item) 输出: python world 98
集合
集合定义
Python语言提供的内置数据结构
与列表、字典一样都属于可变类型的序列,无序序列
集合是没有value的字典
集合的创建方式
- 直接使用
{}
- 使用内置函数
set()
- 定义空集合——
s = set()
'''第一种创建方式,使用{}''' s = {1,2,3,5,5,6,7,7} #集合中元素不允许重复 print(s) # {1, 2, 3, 5, 6, 7} '''第二种创建方式,set()''' s1 = set(range(6)) print(s1,type(s1)) # {0, 1, 2, 3, 4, 5} <class 'set'> #列表转为集合 s2 = set([1,2,3,3,4,5,5]) print(s2,type(s2)) # {1, 2, 3, 4, 5} <class 'set'> #元组转为集合 s3 = set((2,3,5,7,87,3)) print(s3,type(s3)) # {2, 3, 5, 7, 87} <class 'set'> 集合中元素是无序的 #字符串转为集合 s4 = set('python') print(s4,type(s4)) # {'y', 'o', 'n', 'h', 'p', 't'} <class 'set'> #定义空集合 s6 = {} print(s6,type(s6)) #{} <class 'dict'> s7 = set() print(s7,type(s7)) #set() <class 'set'>
集合的相关操作
集合元素的判断
用 in 或 not in 判断元素是否在集合中
集合元素的新增
- add()----一次添加一个元素
- update()----一次至少添加一个元素
集合元素的删除
- 调用remove()----一次删除一个指定的元素,如果指定元素不存在,抛出KeyError
- 调用discard()----一次删除一个指定的元素,如果指定元素不存在,不抛异常
- 调用pop()----一次只删除一个任意元素; 不能删除指定参数
- 调用clear()----清空集合
s = {10,20,30,50,70} '''集合元素的判断操作''' print(10 in s) # TRUE print(100 not in s) # TRUE '''集合元素的新增操作''' s.add(109) print(s) # {70, 10, 109, 50, 20, 30} s.update({123,456,322}) #添加集合 print(s) #{322, 70, 456, 10, 109, 50, 20, 123, 30} s.update([45,32,12]) #添加列表 s.update((78,65,90)) #添加元组 print(s) # {32, 65, 322, 70, 456, 10, 12, 109, 45, 78, 50, 20, 90, 123, 30} '''集合元素的删除操作''' s.remove(322) print(s) #{32, 65, 70, 456, 10, 12, 109, 45, 78, 50, 20, 90, 123, 30} s.discard(456) print(s) #{32, 65, 70, 10, 12, 109, 45, 78, 50, 20, 90, 123, 30} s.pop() print(s) #{65, 70, 10, 12, 109, 45, 78, 50, 20, 90, 123, 30} s.clear() print(s) # set()
集合间的关系
1、两个集合是否相等
使用运算符==或!=进行判断----元素相同,就相等
2、一个集合是否是另一个集合的子集
调用
issubset()
进行判断B是A的子集
ex:print(B.issubset(A))
3、一个集合是否是另一个集合的超集
调用
issuperset()
进行判断A是B的超集
ex:print(A.issuperset(B))
4、两个集合是否没有交集
调用
isdisjoint()
进行判断ex:print(s1.isdisjoint(s2))----s1与s2是否没有交集
没有交集为TRUE,有交集为False
集合的数学操作
1、交集----调用函数
intersection()
或者 使用符号&
2、并集----调用函数
union()
或者 使用符号|
3、差集----调用函数
difference()
或者 使用符号-
4、对称差集----调用函数
symmetric_difference()
或^
集合生成式
用于生成集合的公式:
s = {i*i for i in range(10)}
————无序注意:
- 将{}改为[]就是列表生成式
- 没有元组生成式
总结