python基础教程_学习笔记4:元组

元组

元组不能修改;(可能你已经注意到了:字符串也不能修改。)

创建元组的语法很简单:如果用逗号分隔了一些值,那么你就自动创建了元组。

>>> 1,3,'ab'

(1, 3, 'ab')

 

元组也是(大部分时候是)通过圆括号括起来的。

>>> (1,3,'13')

(1, 3, '13')

 

空元组可以用没有内容的两个圆括号来表示。

 

如何实现包括一个值的元组呢?

>>> (5)

5

>>> ('ab')

'ab'

>>> ([5])

[5]

这样做满足不了需求。

实现方法有些奇特——必须加个逗号,即使只有一个值:

>>> (5,)

(5,)

>>> ('ab',)

('ab',)

>>> ([5],)

([5],)

看来,上一节中存在的疑问部分提到的问题,我找到答案了。

 

其中,逗号是很重要的,只添加圆括号也是没用的:

>>> 3*(5+2)

21

>>> 3*(5+2,)

(7, 7, 7)

 

tuple函数

list函数一样,一一个序列作为参数并把它转换为元组。如果参数就是元组,那么该参数就会被原样返回:

 

字符串

>>> tuple1=tuple('tuple')

>>> tuple1

('t', 'u', 'p', 'l', 'e')

列表

>>> tuple1=tuple([1234,'abc'])

>>> tuple1

(1234, 'abc')

 

单字符字符串

>>> tuple1=tuple('1')

>>> tuple1

('1',)

单元素列表

>>> tuple1=tuple([1234])

>>> tuple1

(1234,)

 

元组

>>> tuple1=tuple(('tup','le'))

>>> tuple1

('tup', 'le')

基本元组操作

元组并不复杂——除了创建元组和访问元组元素外,没用太多其它操作。可以参照其它类型的序列来实现。

 

 

元组的分片还是元组,就如同列表的分片还是列表一样。

 

元组的序列操作

创建元组:

>>> tuple1=tuple('today')

>>> tuple1

('t', 'o', 'd', 'a', 'y')

 

利用索引访问元素:

>>> tuple1[3]

'a'

 

分片获取元素

>>> tuple1[2:]

('d', 'a', 'y')

>>> tuple1[:3]

('t', 'o', 'd')

>>> tuple1[0:4:2]

('t', 'd')

 

获取元组最大值、长度、最小值:

>>> max(tuple1)

'y'

>>> len(tuple1)

5

>>> min(tuple1)

'a'

 

赋值:

>>> tuple1[0]='d'

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'tuple' object does not support item assignment

因为元组是不能被修改的,所以也就不能被赋值。

元组方法

>>> tuple1=tuple('method')

>>> tuple1

('m', 'e', 't', 'h', 'o', 'd')

 

上一节提到列表有九个方法,那么元组是否支持这些方法呢?

append

>>> tuple1.append('a')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'append'

count

>>> tuple1.count('a')

0

>>> tuple1.count('o')

1

extend

>>> tuple1.extend('a')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'extend'

index

>>> tuple1.index('o')

4

同列表一样,当指定的字符不存在时返回错误:

>>> tuple1.index('z')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: tuple.index(x): x not in list

insert

>>> tuple1.insert(2,'ab')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'insert'

pop

>>> tuple1.pop()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'pop'

remove

>>> tuple1.remove('z')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'remove'

reverse

>>> tuple1.reverse()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'reverse'

sort

>>> tuple1.sort()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'sort'

 

通过上述方法的使用可知,凡是可以修改序列的方法元组均不支持,这也正和元组不能修改是一致的。

元组的重要性

有两个重要的原因,使得元组不可替代:

1)元组可以在映射中当做键使用,而列表则不行;

2)元组作为很多内建函数和方法的返回值存在;

一般来说,列表可能更能满足对序列的所有需求。

疑问解答

在列表小节中遇到了几个问题,以下解答一下:

1序列相乘后得到的序列类型不变,但单元素的元组进行乘法后得到的不是元组;

>>> ('string')*5

'stringstringstringstringstring'

>>> (5)*5

25

解答:单元素元组应用逗号隔开。

>>> ('string',)*5

('string', 'string', 'string', 'string', 'string')

>>> (5,)*5

(5, 5, 5, 5, 5)

 

对元组进行成员资格检查,得到的结果与预想不一致;

>>> (2,3,9) in (1,2,3,9,19)

False

解答:in是成员操作符,只有当左值是右值中的成员时才会返回True。以下是True的例子:

>>> (2,3,9) in (1,(2,3,9),19)

True

>>> (2,) in (1,(2,),19)

True

>>> 2 in (2,3,4)

True

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

signjing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值