元组3:方法实操

目录

1. s.index()

2. s.count()

3. 元组拼接:+、+=

4. 元组重复:*、*=

5. 元组的嵌套


元组只支持index、count和元组的拼接(+)、重复(*)。

效果与列表同名方法一致。

1. s.index()

与列表的index()方法一致。

返回 x 元素位于 s 元组的索引值(start 和 end 可选,指定开始和结束位置,不过返回的索引值仍然是以序列开始位置计算的);如果找不到,则返回 ValueError 异常。

s中存在多个相同元素,返回第一个元素的索引值。

s中存在数值相同的整数和浮点数,查询整数或浮点数,都是返回第一个整数或浮点数的索引值。

end如果存在,则start不能为空。

start超过当前s最大索引值,则查询不到元素报错。

end超过当前s最大索引值,则依旧在s范围内查找。

s = 'start0',1,2.0,3,[4,'4',4.0],5,6,'7','8.0',9

#查询元组中的整数、浮点数、字符串、列表
s.index(1)
Out[21]: 1
s.index(2.0)
Out[22]: 2
s.index('8.0')
Out[24]: 8
s.index([4,'4',4.0])
Out[25]: 4

#查询不在元组中的元素
s.index(8.0)
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-23-eb4966855274>", line 1, in <module>
    s.index(8.0)
ValueError: tuple.index(x): x not in tuple

#从元组位置5开始查询,元素不存在
s.index([4,'4',4.0],5)
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-26-16cae263417a>", line 1, in <module>
    s.index([4,'4',4.0],5)
ValueError: tuple.index(x): x not in tuple
#从元组位置0到4-1查询,元素不存在
s.index([4,'4',4.0],0,4)
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-27-34a0f5a93541>", line 1, in <module>
    s.index([4,'4',4.0],0,4)
ValueError: tuple.index(x): x not in tuple
#从元组位置4到5-1查询,元素存在,返回正确结果
s.index([4,'4',4.0],4,5)
Out[28]: 4

#查询起点超过元组元素数,不存在该数
s.index(5,11)
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-29-6f52eabbb4bb>", line 1, in <module>
    s.index(5,11)
ValueError: tuple.index(x): x not in tuple
#查询终点超过元组元素数,存在该数,返回正确结果
s.index(5,0,11)
Out[30]: 5

#查询范围终点若存在,起点不能为空
s.index(5,,11)
  File "<ipython-input-31-2779472b5e8c>", line 1
    s.index(5,,11)
              ^
SyntaxError: invalid syntax
#查询范围起点若存在,终点可以为空
s.index(5,0,11)
Out[30]: 5
 
#数值相同的整数或浮点数,查找时认为等价
s = 0,1,0,1.0
s.index(1)
Out[33]: 1
s.index(1.0)
Out[34]: 1

2. s.count()

与列表的count()方法一致,返回 x 元素在 s 元组中出现的次数。

s = 0,1,0,1.0,'1',0,'11',0,[1]
s.count(0)
Out[39]: 4
s.count(1)
Out[40]: 2
s.count(1.0)
Out[41]: 2
s.count('1')
Out[42]: 1
s.count([1])
Out[43]: 1
s.count('111')
Out[44]: 0

3. 元组拼接:+、+=

#元组的拼接
s = 1,2,3
s1 = 6,7,8
s += s1
s
Out[50]: (1, 2, 3, 6, 7, 8)

#元组不可以直接拼接整数
s = s + 1
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-51-fae459add732>", line 1, in <module>
    s += 1
TypeError: can only concatenate tuple (not "int") to tuple

#注意1,2也是元组,这也是一个元组的拼接
s += 1,2
s
Out[53]: (1, 2, 3, 6, 7, 8, 1, 2)

#元组不带括号时,注意发生问题
#判断这是一个(1,(2+1),2)的元组,而非(1,2) + (1,2)
s = 1,2 + 1,2
s
Out[55]: (1, 3, 2)
#判断这是一个(s+1,2)的元组,而非s + (1,2),s+1不成立报错
s = s + 1,2
Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-56-bfd3af2f2b53>", line 1, in <module>
    s = s + 1,2
TypeError: can only concatenate tuple (not "int") to tuple

4. 元组重复:*、*=

s = (1,2,3)*2
s
Out[61]: (1, 2, 3, 1, 2, 3)
s *= 2
s
Out[63]: (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)

5. 元组的嵌套

s1 = (1,2,3)
s2 = (4,5,6)
s = (s1,s2)
s
Out[69]: ((1, 2, 3), (4, 5, 6))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

燃烧的火鸟啊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值