Python's tuples are similar to lists and can be accessed through an index, supporting heterogeneous, arbitrary nesting. The difference is that elements of the tuple cannot be modified. Tuples use parentheses , and the list uses square brackets .
Creating tuples
Tuple creation is simple, just add elements in parentheses and separate them with commas.
tuple = () #空元组
Tuple2 = (' A ', ' B ', ' C ')
Tuple3 = (' d ', ' e ', ' f ')
You can use Dir (tuple) to view the operations supported by tuples;
Tuple operation method and example display
Count
1 function: Number of elements in a statistic tuple 2 syntax: T.count (value), integer--return number of occurrences of value3 T = (' A ', ' B ', ' C ', ' d ', 1,2,2,3,4 ) 4 T.count (2) 5 results: 2
Index
1 function: Gets the index value of the element in the tuple, for the duplicate element, by default gets the index value of the first element from the left 2 syntax: T.index (value, [Start, [Stop]), integer--return primary index of Valu E.raises ValueError If the value is not present.3 T = (' A ', ' B ', 2, ' C ', ' d ', 1,2,3,4) 4 T.index (2) 5 results: 2 #元素2第一次出现在索引为2的位置6 T.index (2,3,7) 7 results: 6
T1 + T2
1 function: Merge two tuples, return a new tuple, primitive tuple invariant 2 syntax: T = T1 + T2 3 T1 = (' A ', ' B ', ' C ') 4 T2 = (1,2,3,4) 5 T = T1 + T 2 6 Result:
7 Print T 8 (' A ', ' B ', ' C ', 1,2,3,4) 9 print T110 (' A ', ' B ', ' C ') print T212 (1,2,3,4)
T1 * N
1 function: Repeat output tuple n times, return a new tuple, primitive tuple unchanged 2 syntax: T = T1 * N3 T1 = (' A ', ' B ', 4 T = T1 * 35 results: 6 print T7 (' A ', ' B ', ' A ', ' a ', ' B ', ' A ', ' A ', ' B ', ' 8 ') print T19 (' A ', ' B ', and a.
Tuples are immutable, but when mutable elements are nested in a tuple, the mutable element is modifiable, the tuple itself is unchanged, and is viewed using the ID (tuple).
1 T = (' A ', ' B ', ' C ', [1,2,3,4],1,2,3]
2 ID (T)
3 140073510482784 4 Print t[3] 5 [1,2,3,4] 6 t[3].append (5) 7 print t[3] 8 [1,2,3,4,5] 9 print T10 (' A ', ' B ', ' C ', [1,2,3,4,5 ],1,2,3)
ID (T)
12 140073510482784
Tuples Support slicing operations
1 syntax: T[start [, stop[, step]] 2 Example Demo: 3 T = (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ') 4 print t[:] #取所有元素 5 (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ') 6 print t[2:] #取从索引2开始到末尾的元素 7 (' C ', ' d ', ' e ', ' f ', ' g ', ' h ') 8 print t[2:6] #取索引2到6的所有元素, Does not contain an index of 6 9 (' C ', ' d ', ' e ', ' f ') of the print T[2:6:2] #从索引2到6, every other element takes one (' C ', ' e ')
2. A list is a data structure that processes an ordered set of items, that is, a sequence of items that can be stored in a list.
Lists are mutable types of data
2.1 Create List L
Ist1 = []
List2=list ()
List3 = [1, ' A ', (1,), [' Hello,python ']
2.2. The elements in the list can be changed, such as
List.append and delete del list[]
List.remove (list[]), remove is the Delete method for the list.
List3. Press the TAB key to see a list of other methods
Practice:
Existing list
List1 = [' xxxx ', ' B ', 3, ' C ', ' & ', ' A ', 3, ' 3 ', 3, ' AA ', ' 3 ', ' XXXX ']
List2 = [' E ', ' f ', ' G ']
Ask for it to do the following:
1. Remove the middle part of ' XXXX ' to form a new list list3
2. Do a few operations on LIST3
1) Remove Special symbols
2) Statistics 3 number of occurrences in LIST3
3) Use the shortest code to remove elements other than 26 letters in LIST3 (requires only list3 operation)
4) Sort the List3
5) Append ' d ' to the end and append the List2 to the LIST3
3. Existing two variables
A = (' H ',)
b = (' H ')
1) Append A and B to the list3 of the previous question to see what the difference is
2) Convert 1 generated list3 to Narimoto group (extension: Own Search method)
3) Prints the tuple with only one element ' H ', and the index in the tuple generated in 2
3.python tuples and Lists