Introduction
Tuple
1. tuples are a set of data surrounded by parentheses (). Different members are separated by commas. Access through subscript
2. Immutable sequence, which can be viewed as an immutable list. Different from the list, the data in the tuples cannot be changed once it is established (so there is no addition, deletion, modification, and only basic sequence operation similar to the List Operation)
3. Support any type, any nesting, and common sequence operations
4. tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples will not change.
Declaration and use
Copy codeThe Code is as follows:
T = () # Empty tuples
T = (1,) # single element tuples. Note that the comma is required.
T = (1, 2, 3)
1 in t # judgment
2 not in t
# Other basic operations in the same sequence: sharding and Indexing
Print t [0]
Print t [-1]
Print t [: 2]
# It will not affect the original tuples
Print t + () # returns the new element group (, 5)
Print t * 2)
T. index (1)
T. count (1)
# List tuples Conversion
L = [1, 2, 3]
Lt = tuple (l)
Tl = list (lt)
Lt_sorted = sorted (l) # Sort the tuples. The returned result is a list.
# Convert string to tuples (to obtain the sequence of character tuples)
Print tuple ('Hello) # ('h', 'E', 'l', 'l', 'O ')
Tuple does not include append, extend, remove, pop, and other additions, deletions, and changes. tuple does not have find
View help
Copy codeThe Code is as follows:
Help (tuple)
Purpose
1. Assignment
Copy codeThe Code is as follows:
T = 1, 2, 3 # equivalent t = (1, 2, 3)
X, y, z = t # split the sequence. The number of variables on the left must be equal to the length of the sequence on the right.
2. Multiple return values of the Function
Copy codeThe Code is as follows:
Def test ():
Return (1, 2)
X, y = test ()
3. Pass the parameter [force not to change the original sequence]
Copy codeThe Code is as follows:
Def print_list (l ):
T = tuple (l) # Or t = l [:]
Dosomething ()
4. String formatting
Copy codeThe Code is as follows:
Print '% s is % s years old' % ('Tom ', 20)
5. As the dictionary key
Advantages
1. Performance
Tuple is faster than list operations
If you need to define a constant set or a read-only sequence, the only operation is to traverse it continuously. Use tuple instead of list.
Copy codeThe Code is as follows:
>>> A = tuple (range (1000 ))
>>> B = range (1000)
>>> Def test_t ():
... For I in:
... Pass
...
>>> Def test_l ():
... For I in B:
... Pass
...
>>> From timeit import Timer
>>> At = Timer ("test_t ()", "from _ main _ import test_t ")
>>> Bt = Timer ("test_l ()", "from _ main _ import test_l ")
Simple Test
Copy codeThe Code is as follows:
>>> At. repeat (3, 100000)
[1.526214838027954, 1.5191287994384766, 1.5181210041046143]
>>> Bt. repeat (3, 100000)
[1.5545141696929932, 1.557785987854004, 1.5511009693145752]
2. Immutable
Write protection for unnecessary data to make the code more secure
Non-mutable. If an object set is transmitted as a list in a program, it may be changed anywhere. If you use tuples, you cannot
Non-mutable only applies to the top layer of the tuples, not their content. For example, the List inside the tuples can be modified.
Copy codeThe Code is as follows:
L = [1, 2, 3]
T = (1, 2, l)
L. append (4)
Immutable provides some integrity and standardization to ensure that they are not modified and maintain a fixed relationship.
Modification Method
Copy codeThe Code is as follows:
Tuple-> list-> tuple