python元组and集合(7)


前言

python学习笔记 day3(仅供学习使用)


一、什么是元祖

在这里插入图片描述

'''可变序列  列表,字典'''
lst=[10,20,45]
print(id(lst))
lst.append(300)
print(id(lst))
'''不可变序列,字符串,元组'''
s='hello'
print(id(s))
s=s+'world'
print(id(s))
print(s)

运行:

140602126179264
140602126179264
140602173834288
140602173834992
helloworld

1.元祖的创建方式

在这里插入图片描述

'''元组的创建方式'''
'''第一种创建方式,使用()'''
t=('Python','world',98)
print(t)
print(type(t))

t2='Python','world',98  #省略了小括号
print(t2)
print(type(t2))

t3=('Python',)  #如果元组中只有一个元素,  逗号不能省
print(t3)
print(type(t3))

t4='Python',  #单个元素,逗号不省,括号可以去掉
print(t4)
print(type(t4))

'''第二种创建方式,使用内置函数tuple()'''
t1=tuple(('Python','world',98))
print(t1)
print(type(t1))

'''空元组的创建方式'''
'''空列表的创建方式'''
lst=[]
lst1=list()

d={}
d2=dict()

#空元组
t4=()
t5=tuple()

print('空列表',lst,lst1)
print('空字典',d,d2)
print('空元组',t4,t5)

运行:

('Python', 'world', 98)
<class 'tuple'>
('Python', 'world', 98)
<class 'tuple'>
('Python',)
<class 'tuple'>
('Python',)
<class 'tuple'>
('Python', 'world', 98)
<class 'tuple'>
空列表 [] []
空字典 {} {}
空元组 () ()

2.为什么要将元组设置为不可变序列

在这里插入图片描述
注意事项:意思就是说,不允许修改元素中的引用,但是可以修改元组中的元素的添加内容。如:下列代码中可修改list中的元素(t1)的个数和内容 但是不允许修改t0和t2中的内容

t=(10,[20,30],9)
print(t)
print(type(t))
print(t[0],type(t[0]),id(t[0]))
print(t[1],type(t[1]),id(t[1]))
print(t[2],type(t[2]),id(t[2]))
'''尝试将t[1]修改为100'''
print(id(100))
#t[1]=100   #元组是不允许修改元素的
'''由于[20,30]列表,而列表是可变序列,所以可以向列中添加元素,而列表的内存地址不变'''
t[1].append(100)  #向列表中添加元素
print(t,id(t[1]))

运行:

(10, [20, 30], 9)
<class 'tuple'>
10 <class 'int'> 4461910976
[20, 30] <class 'list'> 140291008437184
9 <class 'int'> 4461910944
4461913856
(10, [20, 30, 100], 9) 140291008437184

3.元组的遍历

在这里插入图片描述

二、集合

1.什么是集合

在这里插入图片描述

'''第一种创建方式使用{}'''
s={2,3,4,5,5,6,7,7} #集合中的元素不允许重复
print(s)

'''第二种创建方式使用set()'''

s1=set(range(6))
print(s1,type(s1))

s2=set([1,2,4,5,5,5,6,6])
print(s2,type(s2))

s3=set((1,2,4,4,5,65))  #集合中的元素是元序的
print(s3,type(s3))

s4=set('python')
print(s4,type(s4))

s5=set({12,4,34,55,66,44,4})
print(s5,type(s5))

#定义一个空集合
s6={}   #dict字典类型
print(type(s6))

s7=set()
print(type(s7))

运行:

{2, 3, 4, 5, 6, 7}
{0, 1, 2, 3, 4, 5} <class 'set'>
{1, 2, 4, 5, 6} <class 'set'>
{65, 1, 2, 4, 5} <class 'set'>
{'h', 't', 'o', 'y', 'p', 'n'} <class 'set'>
{34, 66, 4, 55, 12, 44} <class 'set'>
<class 'dict'>
<class 'set'>

2.集合增删改

在这里插入图片描述

s={10,20,30,405,60}
'''集合元素的判断操作'''
print(10 in s)  #True
print(100 in s) # False
print(10 not in s) #False
print(100 not in s) #True
'''集合元素的新增操作'''
s.add(80)  #add一次添加一个元素
print(s)
s.update({200,400,300})  #一次至少添加一个元素
print(s)
s.update([100,99,8])
s.update((78,64,56))
print(s)

'''集合元素的删除操作'''
s.remove(100)
print(s)
#s.remove(500) #KeyError: 500
s.discard(500)
s.discard(300)
print(s)
s.pop()
s.pop()

#s.pop(400) #TypeError: pop() takes no arguments (1 given)不准有参数!!!
print(s)
s.clear()
print(s)

运行:

True
False
False
True
{10, 80, 20, 405, 60, 30}
{200, 10, 300, 80, 400, 20, 405, 60, 30}
{64, 99, 100, 200, 8, 10, 300, 78, 80, 400, 20, 405, 56, 60, 30}
{64, 99, 200, 8, 10, 300, 78, 80, 400, 20, 405, 56, 60, 30}
{64, 99, 200, 8, 10, 78, 80, 400, 20, 405, 56, 60, 30}
{200, 8, 10, 78, 80, 400, 20, 405, 56, 60, 30}
set()

3.集合之间的关系

在这里插入图片描述

'''两个集合是否相等(元素相同,就相等)'''
s={10,20,30,40}
s2={30,40,20,10}
print(s==s2)   #True
print(s!=s2)   #False

'''一个集合是否是另一个集合的子集'''
s1={10,20,30,40,50,60}
s2={10,20,30,40}
s3={10,20,90}
print(s2.issubset(s1))  #True
print(s3.issubset(s1))  #False

'''一个集合是否是另一个集合的超集'''
print(s1.issuperset(s2))  #True
print(s1.issuperset(s3))  #False

'''两个集合是否含有交集'''
print(s2.isdisjoint(s3))  #False   有交集为False
s4={100,200,300}

print(s2.isdisjoint(s4))  #True    没有交集为True

运行:

True
False
True
False
True
False
False
True

4.集合的数据操作

集合的数学操作
在这里插入图片描述

s1={10,20,30,40}
s2={20,30,40,50,60}
print(s1.intersection(s2))
print(s1 & s2)    #intersection()与 & 等价,交集操作

print(s1)
print(s2)
#(2)并集操作
print(s1.union(s2))
print( s1| s2)  #union与  | 等价,并集操作
print(s1)
print(s2)

#(3)差集操作
print(s1.difference(s2))
print(s1-s2)#difference与-是等价的

print(s1)
print(s2)

#(4)对称差集
print(s1.symmetric_difference(s2))#symmetric_difference与^是等价的
print(s1^ s2)

运行:

{40, 20, 30}
{40, 20, 30}
{40, 10, 20, 30}
{40, 50, 20, 60, 30}
{40, 10, 50, 20, 60, 30}
{40, 10, 50, 20, 60, 30}
{40, 10, 20, 30}
{40, 50, 20, 60, 30}
{10}
{10}
{40, 10, 20, 30}
{40, 50, 20, 60, 30}
{50, 10, 60}
{50, 10, 60}

5.集合的生成式

ps:类似于列表

#列表生成式
lst=[ i*i for i in range(10)]
print(lst)


#集合生成式
s={ i*i for i in range(10)}
print(s)


总结

为什么没有元组生成式,因为元组是不可变序列,所以没有
可变序列都可通过生成式来快速生成。如:列表,字典等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小唁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值