Brief introduction
Tuple
1. Tuples are collections of data surrounded by parentheses "()", separated by "," by different members. Access by subscript
2. Immutable sequences, which can be seen as immutable lists, are different from the list: once the data in the tuple is established, it cannot be changed (so there are no additions or deletions to the list, only the 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 take a set of values, that is, the value of the tuple being used does not change
Declaration and use
Copy the Code code as follows:
t = () #空元组
t = (1,) #单个元素元组, note that commas must be
t = (--)
1 in T #判断
2 Not in T
#其他同序列基本操作: Shards, indexes
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 = [n/A]
lt = Tuple (L)
TL = List (LT)
lt_sorted = sorted (l) #对元组进行排序, return is List
#字符串转元组 (get word OFDM group sequence)
Print tuple (' Hello ' # (' h ', ' e ', ' l ', ' l ', ' o ')
Tuple no append/extend/remove/pop and other additions and deletions change the operation tuple no find
View Help
Copy the Code code as follows:
Help (tuple)
Use
1. Assigning values
Copy the Code code as follows:
t = #等价 T = (1, 2, 3)
X, y, z = t #序列拆封, requires equal number of left variable and right sequence length
2. function multiple return values
Copy the Code code as follows:
def test ():
Return (from)
X, y = Test ()
3. Parameter [force does not change original sequence]
Copy the Code code as follows:
def print_list (L):
t = tuple (l) #或者t = l[:]
DoSomething ()
4. String formatting
Copy the Code code as follows:
print '%s '%s years old '% (' Tom ', 20)
5. As a dictionary key
Advantages
1. Performance
Tuple is faster than list operation
If you need to define a constant set, or a read-only sequence, the only operation is to continually traverse it, using a tuple instead of a list
Copy the 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 the 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. immutability
"Write-protect" data that is not needed to make your code more secure
Immutability, if you pass a collection of objects as a list in a program, and may be changed anywhere, using tuples, you cannot
Immutability applies only to the top level of the tuple itself, not its contents, such as a list within a tuple can be modified
Copy the Code code as follows:
L = [n/A]
t = (1,2,l)
L.append (4)
Immutability provides some integrity, normalization, and assurance that it will not be modified to maintain some kind of fixed relationship.
Methods of modification
Copy the Code code as follows:
Tuple, List-a tuple