元组基本操作:索引、切片、循环、长度、包含
创建元组:
1 2 3 | ages = (11, 22, 33, 44, 55) 或 ages = tuple((11, 22, 33, 44, 55)) |
---|
一般写法括号内最后面加个英文逗号用来区分:
test = (,)
test1 = (11,22,)
例: test = (123, 456, 789, 'abc',)
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
Python 表达式 | 结果 | 描述 |
---|---|---|
len((1, 2, 3)) | 3 | 计算元素个数 |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制 |
3 in (1, 2, 3) | True | 元素是否存在 |
for x in (1, 2, 3): print (x,) | 1 2 3 | 迭代 |
因为元组也是一个序列,所以我们可以访问元组中的指定位置的元素,也可以截取索引中的一段元素,如下所示:
元组:
L = ('Google', 'Taobao', 'Runoob')
Python 表达式 | 结果 | 描述 |
---|---|---|
L[2] | 'Runoob' | 读取第三个元素 |
L[-2] | 'Taobao' | 反向读取;读取倒数第二个元素 |
L[1:] | ('Taobao', 'Runoob') | 截取元素,从第二个开始后的所有元素。 |
运行实例如下:
>>> L = ('Google', 'Taobao', 'Runoob')
>>> L[2]
'Runoob'
>>> L[-2]
'Taobao'
>>> L[1:]
('Taobao', 'Runoob')
Python元组包含了以下内置函数
序号 | 方法及描述 | 实例 |
---|---|---|
1 | len(tuple) 计算元组元素个数。 | >>> tuple1 = ('Google', 'Runoob', 'Taobao') >>> len(tuple1) 3 >>> |
2 | max(tuple) 返回元组中元素最大值。 | >>> tuple2 = ('5', '4', '8') >>> max(tuple2) '8' >>> |
3 | min(tuple) 返回元组中元素最小值。 | >>> tuple2 = ('5', '4', '8') >>> min(tuple2) '4' >>> |
4 | tuple(seq) 将列表转换为元组。 | >>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu'] >>> tuple1=tuple(list1) >>> tuple1 ('Google', 'Taobao', 'Runoob', 'Baidu') |
1.通过索引取值
v = test[2] #取出 789
print(v)
2.切片取值
1 2 | v1, v2 = test[1:3] #取出456和789 pritn(v1, v2) |
---|
3.可以for循环,是可迭代对象
1 2 | for item in test: print(item) |
---|
4.元组转化为字符串(元组内元素必须都是字符)
1 2 3 | tu = ('abc', 'efg', 'hij',) tu1 = "".join(tu) print(tu1) |
---|
5.元组转化为列表
1 2 | li = list(test) print(li) |
---|
6.增加元素问题
1 2 3 4 | # tu[2]:元组 tu[3]:列表 tu[3][0]:元组 tu[6]:bool tu = ('nihao', 333, (44, 55,), [(888, 999,)], 54, 45, True) tu[3] = "aa" # 报错,因为tu[3]作为tu元组的一级元组,不可修改,删除 tu[3].append('33') # tu[3]是tu元组的一级元素,只是不能对tu[3]本身进行修改、删除。但是可以对tu[3]进行list的方法 |
---|
7.tuple的count方法:获取指定元素在元组中出现的次数
count(self, value)
参数:value:待查询出现次数的元素
1 2 3 | tu = ('nihao', 333, (44, 55,), [(888, 999,)], 54, 333, True) v = tu.count(333) print(v) |
---|
8.tuple的index方法:获取指定元素的下标(就近原则,从左往右,找到第一个就结束)
index(self, value, start=None, stop=None)
参数:value:待查询下标的元素
start:查询起始下标
stop:查询终止下标(查询到stop前一个下标)
1 2 3 | tu = ('nihao', 333, (44, 55,), [(888, 999,)], 54, 333, True) v = tu.index(333, 4, 7) print(v) |
---|
class tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items
If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return 0
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
def __add__(self, y): # real signature unknown; restored from __doc__
""" x.__add__(y) <==> x+y """
pass
def __contains__(self, y): # real signature unknown; restored from __doc__
""" x.__contains__(y) <==> y in x """
pass
def __eq__(self, y): # real signature unknown; restored from __doc__
""" x.__eq__(y) <==> x==y """
pass
def __getattribute__(self, name): # real signature unknown; restored from __doc__
""" x.__getattribute__('name') <==> x.name """
pass
def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass
def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass
def __getslice__(self, i, j): # real signature unknown; restored from __doc__
"""
x.__getslice__(i, j) <==> x[i:j]
Use of negative indices is not supported.
"""
pass
def __ge__(self, y): # real signature unknown; restored from __doc__
""" x.__ge__(y) <==> x>=y """
pass
def __gt__(self, y): # real signature unknown; restored from __doc__
""" x.__gt__(y) <==> x>y """
pass
def __hash__(self): # real signature unknown; restored from __doc__
""" x.__hash__() <==> hash(x) """
pass
def __init__(self, seq=()): # known special case of tuple.__init__
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items
If the argument is a tuple, the return value is the same object.
# (copied from class doc)
"""
pass
def __iter__(self): # real signature unknown; restored from __doc__
""" x.__iter__() <==> iter(x) """
pass
def __len__(self): # real signature unknown; restored from __doc__
""" x.__len__() <==> len(x) """
pass
def __le__(self, y): # real signature unknown; restored from __doc__
""" x.__le__(y) <==> x<=y """
pass
def __lt__(self, y): # real signature unknown; restored from __doc__
""" x.__lt__(y) <==> x<y """
pass
def __mul__(self, n): # real signature unknown; restored from __doc__
""" x.__mul__(n) <==> x*n """
pass
@staticmethod # known case of __new__
def __new__(S, *more): # real signature unknown; restored from __doc__
""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
pass
def __ne__(self, y): # real signature unknown; restored from __doc__
""" x.__ne__(y) <==> x!=y """
pass
def __repr__(self): # real signature unknown; restored from __doc__
""" x.__repr__() <==> repr(x) """
pass
def __rmul__(self, n): # real signature unknown; restored from __doc__
""" x.__rmul__(n) <==> n*x """
pass
def __sizeof__(self): # real signature unknown; restored from __doc__
""" T.__sizeof__() -- size of T in memory, in bytes """
pass
tuple