元组常用操作

'''元组的方法
t.index()
t.count()
'''

# 元组由于不可更改元组里面的数据(第一层)
# 所以元组可操作的方法比较少

t = (1,)  # 单个元素,为了区分,加个逗号才叫元组
t1 = (3, 4, 'hello', [2, 3, 'ppp'], 3)
print(t1[2])
n1 = t1.count(3)     # count(元组里面的元素)同之前的用法,返回参数这个数据在元组中出现的次数
print(n1)            # 没有的话,则返回0
n2 = t1.index(3)   # index(元组里的元素)从左往右索引,找到了返回下标,没找到则报错
print(n2)
t2 = (('a', 'A'), (2, 2), ('c', 'C'))
for i in t2:
    print(i)

for i, j in t2:   # 要注意一点,拆包过程中组合可以是[( , ), ( , )]或(( , ),( , ))
    print(i)                              # [[ , ],[ , ]]或([ , ],[ , ])
    print(j)

  

猜你喜欢

转载自www.cnblogs.com/wf-skylark/p/9009733.html