tuple 元组

Posted lxcai213

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tuple 元组相关的知识,希望对你有一定的参考价值。

#()  元组
# 元组的索引切片
#
t = (1,2,3,5,6,8,9,5,67,2,3,)
#
print(t)
print(type(t))    #  <class ‘tuple‘>
print(t[0])
print(t[3])
print(t[:3])
print(t[::3])

# tuple
t = (1,2,3,5,6,8,9,5,67,2,3,22,3,6,5,2,3,6)
t = t.count(2)
print(t)
print(t.index(2))


t = (mi,1,3,6,99,5,9,5,niaho)
print(dir(t))       #[‘__add__‘, ‘__class__‘, ‘__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‘]
print(__iter__ in dir(t))  #  True
#   元组可作为可迭代对象
for i in t:
    print(i)

# 元组的拆包
a,b = 5 , 3
a , b = [5,3]
a , b = (5,3)

# 推导式
re0 = [i for i in range(9)]
print(re0)
#[0, 1, 2, 3, 4, 5, 6, 7, 8]
# 生成器
re =  (i for i in range(9))
print(re)   #<generator object <genexpr> at 0x032FBF40>
for i in re:
    print(i)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8

 

以上是关于tuple 元组的主要内容,如果未能解决你的问题,请参考以下文章

详解Python的元组(tuple)的12种操作方法,并附示例代码

python基础之--元组(tuple),python小白必看!

Python 元组(tuple)

Python 元组(tuple)

Tuple(元组)

Python Tuple(元组) tuple()方法