Brief introduction
Tuple
1. Tuples are collections of data surrounded by parentheses "()", separated by "," by different members. Access through subscript
2. Immutable sequence, can be considered as immutable list, and the list is different: the data in the tuple can not be changed once established (so there is no similar list of additions and deletions, only basic sequence operation)
3. Supports arbitrary types, arbitrary nesting, and common sequence operations
4. Tuples are typically used when a statement or user-defined function can safely adopt a set of values, that is, the value of the tuple being used does not change
Declaration and use
Copy Code code as follows:
t = () #空元组
t = (1,) #单个元素元组, note that commas must be
t = (1,2,3)
1 in T #判断
2 Not in T
#其他同序列基本操作: Partitioning, indexing
Print T[0]
Print T[-1]
Print T[:2]
#不会对原来元组造成影响
Print t+ (4,5) #返回新元组 (1,2,3,4,5)
Print T * 2 # (1,2,3,1,2,3)
T.index (1)
T.count (1)
#列表元组转换
L = [1,2,3]
lt = Tuple (L)
TL = List (LT)
lt_sorted = sorted (l) #对元组进行排序, return is List
#字符串转元组 (get word metacharacters group sequence)
Print tuple (' Hello ') # (' H ', ' e ', ' l ', ' l ', ' o ')
Tuple no append/extend/remove/pop and other additions and deletions to the operation tuple no find
View Help
Copy Code code as follows:
Use
1. Assigning value
Copy Code code as follows:
t = 1,2,3 #等价 t = (1, 2, 3)
X, y, z = t #序列拆封, which requires equal number of variables on the left and the right sequence length
2. function multiple return values
Copy Code code as follows:
def test ():
Return (1,2)
X, y = Test ()
3. Parameter [force does not change original sequence]
Copy Code code as follows:
def print_list (L):
t = tuple (l) #或者t = l[:]
DoSomething ()
4. String formatting
Copy Code code as follows:
print '%s '%s years old '% (' Tom ', 20)
5. As the key of the dictionary
Advantages
1. Performance
Tuple faster than list operation
If you need to define a constant set, or a read-only sequence, the only operation is to iterate through it, using tuple instead of the list
Copy Code code as follows:
>>> A = tuple (range (1000))
>>> B = Range (1000)
>>> def test_t ():
... for i in a:
. 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 Code code as follows:
>>> at.repeat (3, 100000)
[1.526214838027954, 1.5191287994384766, 1.5181210041046143]
>>> bt.repeat (3, 100000)
[1.5545141696929932, 1.557785987854004, 1.5511009693145752]
2. Non-variability
Write-protect data that you don't need to make your code more secure
Immutability, if you pass a collection of objects as a list in a program, may be changed anywhere, using tuples, you cannot
Immutability applies only to the top level of the tuple itself, not to its content, such as a list within a tuple that can be modified
Copy Code code as follows:
L = [1,2,3]
t = (1,2,l)
L.append (4)
Immutability provides some kind of integrity, normalization, and assurance that it will not be modified to maintain a fixed relationship
Method of modification
Copy Code code as follows: