Python元组概述
1、元组定义
tuple
:存储有序对象的一种容器
t=(x1,x2,x3…xn)
(1)书写方式
t1=('T3',96,'大数据分析')
t2='T3',90,'数据库原理与应用'
t3=tuple(('T3',96,'大数据分析')) # 这里里面必须在用小括号括起来
t4,t5,t6='T3',90,'数据库原理与应用'
- 第三种t3如果里面不用()会报错:
- 并且第四种的t4,t5,t6分别等于’T3’,90,‘数据库原理与应用’
(2)类型
print(type(t1))
print(type(t2))
print(type(t3))
# <class 'tuple'>
# <class 'tuple'>
# <class 'tuple'>
- 单元素元组的表示一定要记得添加逗号
(3)二维元组
t123=t1,t2,t3
print(type(t123))
t123
t124=(('元组1',100),('元组2'))
t124
(4)单元组
print(type('经济与管理'))
print(type(('经济与管理',))) # 单元素元组的表示一定要记得添加逗号
- 元组的大小和内容不可更改,不可改变元素的顺序,不可对元素进行重新复制
(5)常用函数(cout,index)
index()
功能描述
Python 元组 index()
方法用于从元祖中找出某个指定对象第一个匹配到的索引值,如果这个对象不在元祖中会报一个异常。
- 语法
tuple.index(obj[,start=0[,end=len(tuple)]])
-
参数说明
obj
– 检索某个指定的对象
start
– 可选参数,开始索引,默认为0。(可单独指定)
end
– 可选参数,结束索引,默认为元祖的长度。(不能单独指定) -
返回值
如果元组中包含 检索的对象 返回索引值,否则抛出异常。
count()
功能描述
-
Python中的
count()
方法用于统计某个元素在元组中出现的次数。 -
语法
tuple.count(obj)
- 参数
obj
— 元组中统计的对象
- 返回值
返回元素在元祖中出现的次数。
2、如何查看元组的一些属性
使用dir(tuple)
或者help(tuple)
['__add__',
'__class__',
'__class_getitem__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']
Help on class tuple in module builtins:
class tuple(object)
| tuple(iterable=(), /)
|
| Built-in immutable sequence.
|
| If no argument is given, the constructor returns an empty tuple.
| If iterable is specified the tuple is initialized from iterable's items.
|
| If the argument is a tuple, the return value is the same object.
|
| Built-in subclasses:
| asyncgen_hooks
| UnraisableHookArgs
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __getnewargs__(self, /)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __rmul__(self, value, /)
| Return value*self.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __class_getitem__(...) from builtins.type
| See PEP 585
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
3、元组的索引和切片
- 将元组变成列表
List
tl01=list(t123)
# [('T3', 96, '大数据分析'), ('T3', 90, '数据库原理与应用'), ('T3', 96, '大数据分析')]
4、元组的不可修改性
immutable
:不可修改性
不能直接给元组的元素进行赋值
例外:如果元组的元素是可修改的对象,如list
,则其值是可修改的
- 对整个元组重新赋值是可以的,id内存也会改变,但如果只是单单对元组的一个元素进行赋值,则必须要保证该元素可修改
5、元组常见操作
x,y=1,2
print(type(x)) # <class 'int'>
x,_=1,2
print(_) # 2
特殊用法
比较以下1和2:特殊用法
6、元组运算
(1)算术运算符
加法
乘法
(2)比较运算符
t1=(1,2,3)
t2=(3,4,5)
t3=('1','2','3')
t4=('a','b','c')
print(t1<t2) # True
print(t3<t4) # True
(3)成员运算符
(4)身份运算符
尽管两者的类型和内容一样,但 is
的结果还是False
7、元组和列表的对比
二者对比的本质是可更改与不可更改的对比
(1)运行效率
%%timeit # 单元格魔术函数需要编写在单元格首行
l1=[]
x=range(10000)
for item in x:
l1.append(item)
%%timeit # 单元格魔术函数需要编写在单元格首行
t1=()
x=range(10000)
for item in x:
t1=t1+(item,)
(2)查错难度
如下图所示:由于list的元素可更改,导致t1[ 0 ]更改,因此造成查错上面的一定难度,而元组的元素不可更改,因此不会有这样的错误