In Python, the list and element group are a Data Structure: A sequence. Each element in the sequence is assigned a sequence number. The position of the element. The position of the first original element is 0, and so on, sequence is the most basic data structure. This article will share with you the first article (2) about the first knowledge list and metadata in Python. If you are interested, let's learn about it in Python, A list and a metadata group are a Data Structure: A sequence. Each element in a sequence is assigned a sequence number. The position of the element is 0 for the first original element, and so on. Sequence is the most basic data structure. The list and its meta-groups have some differences, that is, the list can be modified, while the tuples cannot be modified! It is best to use a list if you want to add elements.
1. in pythonThe most basic data structure is a sequence. Each element in the sequence is assigned a sequence number, that is, the position of the element, which is called an index. The index starts from 0.-1 indicates the first to last element, and-2 indicates the second to last element. Therefore, you can access the element from the beginning to the end, you can also access the elements from the back.
2. python SequenceThe column contains six built-in sequences: List, tuples, strings, Unicode strings, buffer objects, and xrange objects. There are two common types: List and metadata.
The main difference between a list and a group is that a list can be modified, but a list cannot be modified. In general, the list can replace tuples in almost all cases. One exception: Use tuples as Dictionary keys. The list cannot be used because the key cannot be modified.
3. ListThe elements are separated by commas and written in square brackets.
4. In sequenceIt can also contain other sequences.
5. General SequenceOperation: Index, partition, add, multiply, check whether an element belongs to a Sequence member, calculate the sequence length, find the maximum element, and find the minimum element.
6. Index:
All the elements in the sequence are numbered. They are incremented from 0 and accessed by numbers respectively.
You can also trace back from the last element. The position of the last element is-1.
The string literal value can also be directly indexed without a variable to reference them. The effects of the two are the same.
If a function call returns a sequence, you can index the returned result directly.
7. Parts:
Use indexes to access a single element in a sequence,
With sharding, you can access multiple elements within a certain range.
The sharding operation requires two indexes as the boundary. The 1st index elements are included in the shard, and the 2nd index does not.
How to access the last element?
There are only nine elements in the original sequence, and the index ranges from 0 to 8.
The last element cannot be obtained using number [].
The last element cannot be obtained either by using the reciprocal number [-3:-1 ].
There are two ways to make a line:
① Set the next boundary Index out of the range. For example, number [] can cover 8th elements.
② Directly omit the next boundary index, such as number [6:], which means that the index can be read from 6th to the last one.
Likewise, there are:
If both the boundary indexes are empty, you can read all the elements.
In addition, if [] Null list is displayed for number [-], the reason is that the previous boundary index element must be on the left side of the next index element; otherwise, the result is null.
8. Set step size for parts
The part actually contains three parameters: [boundary 1: Boundary 2: Step Size]
If the step size parameter is omitted, the default value is 1.
If the step size is 1, all elements in the boundary are convenient.
When the step size is greater than 1, elements are retrieved at intervals.
For example, if the step is set to 2, the first element is taken for every two elements, or one element is taken for every one element.
The step size cannot be 0, but the step size can be a negative number. In this case, it is a reverse fetch, which is the number from right to left, the corresponding boundary index is no longer the requirement that the first index element must be on the left of the 2nd index, but that the first index element must be on the right of the second index element.
For example:
Summary:
① The step size parameter can be omitted. If the parameter is omitted, the default value is 1;
② When the step size is positive, it is obtained from the left side of the sequence to the right. The result is also in this order. When the step size is negative, it is obtained from the right side to the left side of the sequence, the results are also arranged in this descending order (relative to the original sequence;
③ When the step size is positive, the first index element must be on the left of the second index element. When the step size is negative, the first index element must be on the right of the second index element;
④ Whether the step size is positive or the step size is negative, the first index element will be taken out, and the second index element will not be taken out.
9. Sequence addition:
You can add two sequences of the same type.
For example:
The string is added to the list. Strings cannot be added to the list.
10. Sequential multiplication:
Multiply the number x by a sequence to generate a new sequence of the same type, and the original sequence of the new sequence will be repeated x times.
If you want to initialize an empty sequence of 10 elements, you can use multiplication. Here you also need to use a python built-in None value.
None indicates nothing.
Remember that python is case sensitive.
11. Check for member existence:
Check whether an element exists in a sequence with in. If yes, true is returned. If no, false is returned.
In the above two examples, one can be used for permission check and the other can be used for user name single check.
Write a user name and password check:
12. length, minimum value, and maximum value:
Use the built-in functions len (), max (), and min () in python to obtain the sequence length, maximum value, and minimum value. For example:
13. List
The list can use all standard operations applicable to sequences, such as indexing, partitioning, addition, multiplication, and so on.
The difference between the List and other types of sequences such as strings and tuples is that the list can be modified.
Therefore, the List has some special operations: negative element values, element deletion, partition assignment, and list methods.
(1) list () function:
The list () function applies to all types of sequences, not just strings.
(2) list assignment:
A list value cannot be assigned to an element that does not exist.
(3) Delete an element
Delete an element from the list and use the del statement.
(4) multipart assignment
In the preceding example, the list function is used to assign values to the partition name [7:], changing the values of multiple elements in the list name at one time.
① Multipart assignment can also be used to replace elements.
In the preceding example, an equi-long list is used to replace the medium-long number of elements in the meta-list. However, when there are not many elements in the substitution list, a direct replacement operation is performed. Here, we use 'World' and '123' to replace the name list.
② Multipart assignment can also complete the insert element operation.
When no partition element is replaced, the value assignment changes to directly inserting the element.
③ Multipart assignment can also delete elements.
Using an empty list to assign values to a shard is equivalent to directly deleting all elements in the shard.
This statement is similar to del number [2: 7].
(5) List Method: append
The append method is used to append a new object to the end of the list.
Format: object. append (parameter)
There can be only one parameter, which is the element to be appended to this object.
(6) List Method: count
The count method is used to count the number of times an element appears in the list.
(7) List Method: extend
The extend method is mainly used to extend another list using one list.
Add the list2 extension directly to the end of list1.
The extended operation differs from the connection operation in that the extended Operation modifies the original list, and the connection operation returns a new list.
The addition operation does not change the original list.
(8) List Method: index
The index method is used to locate the index location of the first matching item of a value from the list.
Index searches for the location of the first 'aaa' in the list and returns its index value.
If this object is not found, in other words, the object is not displayed in the list, an error will be reported for a long time and an exception will occur.
(9) List Method: insert
The insert method is used to insert objects into the list.
(10) List Method: pop
The pop method is used to remove an element from the list and return the value of this element.
By default, the last element is removed.
If the parameter value in pop (parameter) is not specified, the last element is automatically removed. If the parameter value is specified, the specified element is removed.
The pop method returns the element to be removed and removes it from the list.
Pop is equivalent to the output stack in the stack, and append is equivalent to the inbound stack in the stack.
Pop (0) can realize the first-in-first-out in the queue.
(11) List Method: remove
The remove method is used to remove the first match of a value in the list.
Only the first match will be removed. No return value. This is different from pop. When the removed item does not exist from the list, the List itself is not changed and an error is returned.
(12) List Method: reverse
The reverse method reversely stores the elements in the list.
If you only want to output the list in reverse iteration without changing the List itself, you can use the reversed () function.
(13) List Method: sort
Sort the list and change the List itself.
English letters and numbers are sorted by ASCII code.
If you want to sort the list without changing the List itself. There are:
If you want to assign a value, assign the x. sort () result to y, but y returns NULL. This assignment is not feasible because the sort method does not return any value.
If you first assign a value to x, assign it to y, and then perform the sort () method on y, you want to sort y by this method without affecting x itself. However, the result shows that the sort of y still affects x. The reason is: in the form of y = x, a new list is not actually generated and assigned to y. Instead, y is directed to the same list as x, that is, both x and y point to the same list. In fact, sorting y is also the list pointed to by x, which also changes x.
It is correct to make x generate a copy and sort the copy without affecting the idea of x itself, but the assignment format is not proper.
You can quickly copy a list to another variable through the sharding operation.
You can use the sorted function to obtain copies of sorted lists without affecting the List itself.
Sorted () is a function, not a method. Therefore, it cannot be used like x. sotred.
The sorted () function can be applied to any sequence.
By default, the sort method is sorted in ascending order. You can use the built-in cmp () function to customize the direction of sorting.
The cmp (x, y) function is used to compare two objects, if x Y returns 1.
The sort method also has two optional parameters: key and reverse.
14. tuples:
Tuples and lists are all sequences. The only difference is that tuples cannot be modified.
The list is enclosed by square brackets, while the tuples are enclosed by parentheses.
The empty list is [], and the empty tuples are ()
A single element's tuples (1,) ----- also needs a comma, which is very important
Tuples of multiple elements (1, 2, 4)
If an element is written as (1), it is actually a number of 1 instead of a tuple.
(1) tuple Function
Similar to the list () function in the list
When using the tuple function, you can split strings and lists to form tuples.
(2) create a tuples
Use the tuple () function to create
Create by Column
(3) Access tuples and tuples for sharding
After the tuples are sharded, they are still tuples.
(4) In general, the list can be used to replace the tuples without changing the content itself.
However, in both cases, tuples are irreplaceable.
① Tuples can be used as keys in ing, but the list does not work.
② Tuples exist as the return values of many built-in functions and methods.
15. Summary:
Sequence: a data structure where the number of elements starts from 0 and the number-1 indicates the last element.
Typical sequences are lists, strings, and tuples. The list can be modified, while the string and element group cannot be modified. Once created, the list is fixed.
You can access a single element or multiple elements of a sequence through multipart operations. You must specify the start position and end position for a shard. The starting position element is included in the shard, And the ending position element is not included in the shard.