Python tuples traversal and sorting methods
In Python, the data type is unchangeable.A key role is tuple ). If an object is defined as a tuples, it means its value cannot be modified unless a new object is defined. Tuples and List lists are often put together for comparison. They are all sequences, so there are many identical operation methods, but the former is an immutable data type, and the latter is a mutable data type, essentially, they are not the same.
Memory Structure
How to Create tuple
1,The method for creating tuples is simple. tuples are included in parentheses (), and the elements in brackets () are separated by commas (,). This completes the creation of tuples.
>>> (1, 2, 3)
(1, 2, 3)
If no brackets () are used during creation, python will also recognize it as a tuple if it is separated by commas (,).
>>> 1, 2, 3
(1, 2, 3)
If the newly created tuples only have one value, remember to add a comma behind the value.
>>> 1,
(1 ,)
If there is no comma, python only treats it as an integer.
>>> 1
1
2,There is also an interesting method to create tuples, which requires the tuple function: it can be a list of parameters and convert it to a tuples.
Tuple ([1, 2, 4])
>>> (1, 2, 3, 4)
Tuple ('hello ')
>>> ('H', 'E', 'l', 'l', 'O ')
The string is used as the parameter, and the result isEveryAll are added to the new element group as a new element.
How to read data and values in tuples
Tuples are an ordered set, which can be offset as the list. In short, tuples support indexing and slicing.
Index (subscript) and slice values:
X = (0, 1, 2, 3)
X [1] index subscript
X [] Slice values do not include four
How to modify the operation method of tuples
Convert a = (, 3) to (, 3)
List (a) converts tuples to list a = [2, 3]
A [0] = 3 change the tuples to the first value of the List
Tuple ()
1. First, input the value of variable a in the list () method and assign the value to variable B to obtain the list B = [2, 3, 3].
2. Take the 0th-bit value of list variable B and change it to 3. In this case, variable B = [3, 3, 3]
3. input the value of variable B in the tupel () method and assign the value to variable a again. At this time, a = (, 3)
If the element contained in the element group has a variable data type, It is very convenient to modify it.
A 0th-bit subscript corresponds to a list. because the list is of a variable data type, you can directly modify it.
1. tuples are an ordered set,
2. You can use indexes and slices to set values for tuples and lists.
3. You cannot modify or replace a tuple in the original place after it is created.
4. tuples support nesting and can contain lists, dictionaries, and different tuples.
5. tuples support general sequence operations, such as: + and ,*
>>> (1, 2) + (3, 4)
(1, 2, 3, 4)
>>> (7, 8) * 4
(7,8, 7,8, 7,8, 7,8)
Note: +, * during the operation, a new Meta Group is returned.