Sequence (list and metadata)
2.2 General sequence operations
1) Index
From left to right (first) is 0, from right to left (last)-1.
Indexes can be directly used for strings: "Hello" [2]
2) fragment
Two boundaries are provided as indexes. The first boundary is included, and the second boundary is not included.
Numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Elegant shortcuts:
Numbers [-3:-1] [8, 9]
Numbers [-] [] the left index must appear earlier than the right index before it is not an empty set.
Numbers [-3:] does not fill in the boundary to indicate to the most boundary
Numbers [:] entire list
Step Size:
The default step size is 1. If not 1, you must specify
Numbers [M: N: K] specifies the step size to K.
The step size cannot be 0, but the step size can be negative. If it is a negative number, the left and right boundary of the list should be from right to left.
3) sequence addition:
[1, 2, 3] + ["hello"] [1, 2, 3, 'Hello']
However, the list and string cannot be added together.
4) multiplication:
"Python" * 2 "pythonpython"
[42] * 3 [, 42]
None empty list Initialization
5) Membership: In
Returns a Boolean value: whether an object is a member of a sequence.
6) Length
Built-in functions Len, Max, Min
-----------------------------------------------------------------------------
2.3 list:
1. List Function
Converts a string or other series to a list.
Because the string is unchangeable, it can be changed after being converted to the list:
List ("hello") ['h', 'E', 'l', 'l', 'O']
PS: You can use ''. Join (somelist) to convert a list somelist to a string.
2. Basic list operations
1) Change the list: Element assignment
You cannot assign a value to a nonexistent index location.
2) delete an element
Del Names [2]
After deletion, the list length is automatically reduced and the order is maintained.
3) multipart assignment
Assign values to multiple elements at a time
A. Note that when a normal value is assigned, a value cannot be assigned to an index that does not exist. However, a multipart value assignment can be used to replace the sharding with a sequence that is not long as the original shard:
Name = List ('perl ')
Name [1:] = List ('ython ')
B can insert a sequence by replacing the partition value of an empty part.
C. Similarly, assigning an empty part to a part can be used to delete the part.
3. List Method
1) append
Lst. append (XXX): note that no return value is returned!
2) count
Count the number of times an element appears in the list: lst. Count (XXX)
3) Extend
Append multiple values to the end of a sequence
A. Extend (B) appends B to the end of a, and a changes
4) Index
Names. Index ('jmz ')
If the index of the first matching item in the list is found, an exception is thrown.
5) Insert
Names. insert (5, 'jmz ')
Insert the name 'jmz' to the index position 5.
6) Pop
Lst. Pop () removes the last element and returns the removed element.
The pop method is the only method that can modify the list and return elements. Note that the methods used to modify the list in the preceding method do not return values.
7) Remove
Names. Remove ('jmz') Delete the first matched Project
8) reverse
Names. Reverse () stores list elements in reverse order
9) sort
Note that no return value is returned.
X. Sort () x becomes ordered, but this expression does not return values.
Y = x. sort does not assign the order form of X to Y.
You can do this:
Y = x [:]
Y. Sort ()
Or:
Y = sorted (X)
To implement the same function
10) advanced sorting
CMP parameter to specify the sorting method: numbers. Sort (CMP)
Define your own CMP function. If the value is less than, a negative number is returned. If the value is greater than, a positive number is returned. If the value is equal, 0 is returned.
Key parameter specifies the sort key: names. Sort (Key = Len) according to the length
The reverse parameter specifies whether to reverse sort: Number. Sort (reverse = true)
-----------------------------------------------------------------------------
2.4 tuples: immutable Sequence
If you use commas to separate values, the tuples are automatically created.
1) tuple Function
Converts a string and a sequence to a tuple. If the converted string is a tuple, the string is returned as is.
Tuple ([1, 2, 3]) (1, 2, 3)
Tuple ('hello') ('h', 'E', 'l', 'l', 'O ')
Tuple (1, 2, 3) (1, 2, 3)
-----------------------------------------------------------------------------
Summary:
List: [] Variable Sequence
Tuples: () immutable Sequence
Some methods