码迷,mamicode.com
首页 > 其他好文 > 详细

tuple 元组

时间:2020-07-17 19:21:55      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:type   sizeof   tor   add   orm   iter   getattr   att   range   

#()  元组
# 元组的索引切片
#
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 元组

标签:type   sizeof   tor   add   orm   iter   getattr   att   range   

原文地址:https://www.cnblogs.com/lxcai213/p/13330089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!