Tuples of Python data structures

an ordered set of elements

Use parentheses ( ) to indicate

Tuples are immutable objects

 

definition

tuple() -> empty tuple

tuple(iterable) -> tuple initialized from iterable's items

t = (1,) # Definition of a tuple of elements, note that there is a comma

 

access to tuple elements

• Support for indexing (subscripting)

• Positive index: Numbers each element in the list from left to right, starting at 0

• Negative indices: right to left, starting at -1

• The positive and negative indexes cannot exceed the bounds, otherwise an exception IndexError will be raised

tuple[index] , index is the index, use brackets to access

 

Tuple query

index(value,[start,[stop]])

• By value value, find whether the elements in the list match from the specified range

• Return the index immediately after the first match

• If it doesn't match, throw an exception ValueError

count(value)

• Returns the number of times the value in the list matches

time complexity

• Both the index and count methods are O(n)

• Efficiency decreases as the size of the list data increases

flax

• returns the number of elements

 

Tuples are read-only, so there are no add, modify, or delete methods.

Guess you like

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