Common operations on tuples

''' tuple method
t.index()
t.count()
'''

# Tuple because the data in the tuple cannot be changed (the first layer)
# So there are fewer tuples that can be manipulated

t = (1,) # A single element, in order to distinguish, add a comma to call a tuple
t1 = (3, 4, 'hello', [2, 3, 'ppp'], 3)
print(t1[2])
n1 = t1.count(3) # count (elements in the tuple) is the same as the previous usage, returns the number of times the data of the parameter appears in the tuple
print(n1) # If not, return 0
n2 = t1.index(3) # index (elements in the tuple) is indexed from left to right, if the returned subscript is found, if not found, an error will be reported
print(n2)
t2 = (('a', 'A'), (2, 2), ('c', 'C'))
for i in t2:
    print(i)

for i, j in t2: # Note that the combination during unpacking can be [( , ), ( , )] or (( , ),( , ))
    print(i) # [[ , ],[ , ]] or ([ , ],[ , ])
    print(j)

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852241&siteId=291194637