Python:元组与集合

简介: Python:元组与集合

元组


元组的定义

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

元组的创建方式

  1. 直接使用() ——小括号可以省略
  2. 使用内置函数 tuple()
  3. 元组中只有一个元素时,使用逗号和小括号,逗号不能省
'''第一种创建方式,使用()'''
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)      #空元组 () ()


元组不可变原因

为什么要将元组设计成不可变序列?

  • 在多任务环境下,同时操作对象时不需要加锁,因此在程序中尽量使用不可变序列


注意事项:

元组中存储的是对象的引用

  1. 如果元组中对象不可变,则不能再引用其他对象,即不可增删元素;
  2. 如果元组中对象可变,则可变对象的引用不允许改变,但该可变对象中数据可以进行增删操作;
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的字典


集合的创建方式

  1. 直接使用{}
  2. 使用内置函数set()
  3. 定义空集合——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 判断元素是否在集合中


集合元素的新增

  1. add()----一次添加一个元素
  2. update()----一次至少添加一个元素


集合元素的删除

  1. 调用remove()----一次删除一个指定的元素,如果指定元素不存在,抛出KeyError
  2. 调用discard()----一次删除一个指定的元素,如果指定元素不存在,不抛异常
  3. 调用pop()----一次只删除一个任意元素; 不能删除指定参数
  4. 调用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()^

0abce994431243d6ac14ddf02e7c3489.png


集合生成式

用于生成集合的公式:

s = {i*i for i in range(10)} ————无序

注意:

  1. 将{}改为[]就是列表生成式
  2. 没有元组生成式


总结

9d3a9704d1c44e4b8c82529e9a394b42.png

c8da8d4cfaad4f7195fcabdb6bb86b22.png

相关文章
|
6天前
|
存储 API 索引
Python 的集合是怎么实现的?
Python 的集合是怎么实现的?
28 9
|
7天前
|
存储 索引 Python
Python常用数据结构——集合
Python常用数据结构——集合
21 3
|
8天前
|
存储 数据处理 Python
Python中的Set集合:高效数据处理的利器
Python中的Set集合:高效数据处理的利器
16 0
|
9天前
|
Python
python推导式-列表,元组,字典,集合推导式
这篇文章介绍了Python中的推导式,包括列表推导式、元组推导式、字典推导式和集合推导式,提供了它们的基本格式和示例代码,并解释了推导式如何简化循环和条件判断的代码编写。
|
2月前
|
数据采集 编解码 算法
Github | 推荐一个Python脚本集合项目
Github | 推荐一个Python脚本集合项目
|
2月前
|
索引 Python 容器
为什么Python中会有集合set类型?
为什么Python中会有集合set类型?
|
2月前
|
存储 缓存 索引
python 的 tuple(元组) 是不是冗余设计?
python 的 tuple(元组) 是不是冗余设计?
|
2月前
|
存储 Python
Python 中的列表和元组
【8月更文挑战第29天】
25 1
|
2月前
|
Python
Python多维列表(元组)合并成一维形式
Python多维列表(元组)合并成一维形式
16 2
|
2月前
|
存储 缓存 安全
Python元组之不可变序列的奥秘与应用方式
Python 中的元组(Tuple)是一种有序的、不可变的数据结构,它是序列的一种特殊形式,就像一个固定大小的盒子,一旦放入物品就无法更换或移除。 元组可以包含任何类型的数据,如数字、字符串甚至是其他元组。 相比列表,元组在很多场景下提供了更高效、安全的选择。
下一篇
无影云桌面