python 元组

任意对象的有序集合

通过偏移存取

属于不可变序列类型

固定,异构,任意嵌套

对象引用的数组

print((1,2)+(3,4)) # (1, 2, 3, 4)
print((1,2)*4) # (1, 2, 1, 2, 1, 2, 1, 2)
t = (1, 2, 3, 4)
print(t[0],t[1:3]) # 1 (2, 3)
print(40,type((40))) # 40 <class 'int'>
print((40,),type((40,))) # (40,) <class 'tuple'>
t = ('cc', 'aa', 'dd', 'bb')
tmp = list(t)
print(tmp) # ['cc', 'aa', 'dd', 'bb']
tmp.sort()
print(tmp) # ['aa', 'bb', 'cc', 'dd']
t = tuple(tmp)
print(t) # ('aa', 'bb', 'cc', 'dd')
print(sorted(t)) # ['aa', 'bb', 'cc', 'dd']
t = (1, 2, 3, 4, 5)
print([x + 20 for x in t]) # [21, 22, 23, 24, 25]
t = (1, 2, 2, 4, 2)
print(t.index(2)) # 1
print(t.index(2, 2)) # 2
print(t.count(2)) # 3
t = (1, [2, 3], 4)
t[1][0] = 'spam'
print(t) # (1, ['spam', 3], 4)

posted on 2019-07-11 08:10  幻嘤剑舞  阅读(91)  评论(0编辑  收藏  举报

导航