0X00 list and metadata
In Python, there is something called sequence, which can be imagined as a pile of data. Data structures such as arrays, linked lists, stacks, and queues can be implemented through sequences.
There are several sequences. Common examples are list and metadata.
0X01 sequential partitioning
We can extract a part from the sequence. This operation is called a part.
We can select the start and end points, the step size, and even the inverted order when sharding.
Used for sharding: separates parameters. Generally, there are two parameters. The first parameter is truncated to the second parameter, and the parameter is left open and right closed.
If the parameter is a negative number, it indicates the last few
However, the third parameter is acceptable, and the third parameter indicates the step size. If the second parameter is 2, it is the next hop.
If the parameter is null, it indicates the limit. For details, refer to the following code:
>>> Username = 'Hello, world'
>>> Print username [] # extract from 4 to 8, left open and right closed
O, wo
>>> Print username [4:-2] # intercept the 4-to-4th parameters. To include the last one,-1 is not supported. Use the following method:
O, wor
>>> Print username [2:] #-1 cannot be used for intercepting the last one, because-1 is the last one, and then the interval is left open and right closed, leave all the values left on the right to indicate the limit.
Llo, world
>>> Print username [:] # The two ends take the limit, which is the complete sequence.
Hello, world
>>> Print username [] # Demo step, where the step size is 2
El, o
>>> Print username [8: 0:-1] # when the step is-1
Row, olle
0X02 sequence splicing
The concatenation of sequences is similar to the concatenation of strings in Java, and can be simply connected with a plus sign. Of course, Python is more convenient than Java. It is not just a string, but anything that can be spliced together in a sequence.
In Python, the plus sign is used to concatenate a sequence. Instead of directly modifying a sequence, a new sequence is returned.
>>> Username = 'Hello'
>>> Password = 'world'
>>> Print username + ',' + password
Hello, world
Sequences can be used not only for addition, but also for multiplication. Returns a sequence that has been repeated for n times.
>>> Username = 'Hello, world'
>>> Print username * 3
Hello, worldhello, worldhello, world
0X03 empty sequence
The empty sequence is empty, rather than the value 0. Maybe I don't know what it is for now. When it comes to use, it will suddenly become clear.
Username = [None] * 10 # A null sequence with a length of 10 is generated.
0X04 Member judgment
A member's judgment is to determine whether an element exists in a sequence.
Here, a Boolean value, True, or False is returned.
>>> Username = 'Hello, world'
>>> ',' In username # judge whether the element is in the sequence
True
>>> 'Hello' in username # judge whether the sequence is in the sequence
True
>>> Username = ['hello', 'World']
>>> 'Hello' in username
True
>>> 'HEL' in username
False
0X05 length & statistics
The length of a sequence can be counted, and the maximum and minimum values of all elements in the sequence can be calculated.
You can search for the specific sorting method online or try it yourself. The sorting method for each type is different.
>>> Username = 'Hello, world' # get the length
>>> Print len (username)
12
>>> Number = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Print max (number) # maximum statistics
9
>>> Print min (number) # average statistics
1
0X06 list assignment
Assigning values to a list is almost the same as assigning values to arrays in other programming languages.
>>> Username = [0, 1, 2, 3, 4, 5]
>>> Username [3] = 33
>>> Username [5] = 55
>>> Print username
[0, 1, 2, 33, 4, 55]
0X07 list delete data
It is easy to understand how to delete data in the list.
>>> Username = ['H', 'e', 'L', 'L', 'O', ',', 'W', 'O ', 'R', 'L', 'D']
>>> Del username [5] # Delete the elements with an index of 5, that is, 6th
>>> Print username
['H', 'e', 'L', 'L', 'O', 'W', 'O', 'R', 'L ', 'D'] # The comma at the original position of 5 is no longer
0X08 list partition assignment
Partition assignment is equivalent to covering some of the previous data and writing new data.
>>> Username = list ('Hello, world ')
>>> Username [] = list ('++ ')
>>> Username [2] = list ('------')
>>> Print username # data added successfully
['H', '+', '-', '+ ', '+', 'W', 'O', 'R', 'L', 'D']
>>> Username [] = [] # theoretically, you can delete the data in the list in this way. However, it is not recommended that you do this. No one wants to read this code, including yourself a few days later
0X09 list common methods
Append add data ---- single
Insert new data at the end of the list and directly modify the previous list, instead of returning a new
>>> Username = [1, 2, 3, 4, 5]
>>> Password = [6, 7, 8, 9, 0]
>>> Username. append (password)
>>> Print username
[1, 2, 3, 4, 5, [6, 7, 8, 9, 0]
Count statistics
Count, count how many times a list appears in another list
>>> Username = 'Hello, world'
>>> Username. count ('L ')
3
>>> Username = ['hello', 'hello', 'World', 'test']
>>> Username. count ('Hello ')
2
Extend add data ---- multiple
Insert data to the end and modify the previous list without returning a new list.
>>> Username = [1, 2, 3, 4, 5]
>>> Password = [6, 7, 8, 9, 0]
>>> Username. extend (password)
>>>>>> Print username
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Index Search
Find the first matched location and return the index location. If 0 is returned, it is found at 0, rather than not. If no exception is found, an exception is thrown directly.
>>> Username = 'Hello, world'
>>> Username. index ('L') # return position
2
>>> Username. index ('h') # returns the position 0.
0
>>> Username. index ('x') # cannot be found. An exception is thrown.
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Insert data ---- exact location
Insert data to the list. Optional parameters include the insert position and content.
>>> Username = [1, 2, 3, 4, 5, 6, 7]
>>> Username. insert (3,666)
>>> Print username # insert 666 to position 3
[1, 2, 3,666, 4, 5, 6, 7]
Pop-up data ---- output stack
The last data in the list is popped up, returned, and deleted. If you know the stack in the data structure, you can understand it.
>>> Username = [1, 2, 3, 4, 5, 6, 7, 8]
>>> Username. pop () # output stack
8
>>> Username. pop (2) # select Delete
3
Remove match delete
Remove the first matched item
>>> Username = [1, 2, 3, 4, 5, 6]
>>> Username. remove (2)
>>> Print username
[1, 3, 4, 5, 6]
Sort sorting method
You can use Python built-in algorithms to sort data or even customize parameters.
>>> Username = [1, 5, 2, 5, 65, 23,546 75, 8, 34,556 8, 345]
>>> Username. sort ()
>>> Print username
[1, 2, 5, 5, 8, 23, 34, 65,345,556 8, 54675]
Sorted sorting function
Similar to sort, but this returns a new list.
>>> Username = [1, 5, 2,234,346 5, 234,465 7, 5, 65, 23,546 75, 8, 34,556 8, 345]
>>> Sorted (username)
[1, 2, 5, 5, 8, 23, 34, 65,234,234,345,346 5, 4657,556 8, 54675]
List (reversed (x) reverse sorting
Reverse
>>> Username = [1, 5, 2,234,346 5, 234,465 7, 5, 65, 23,546 75, 8, 34,556 8, 345]
>>> Username = sorted (username)
>>> Print username
[1, 2, 5, 5, 8, 23, 34, 65,234,234,345,346 5, 4657,556 8, 54675]
>>> List (reversed (username ))
[54675,556 8, 4657,346 5, 345,234,234, 65, 34, 23, 8, 5, 5, 2, 1]
0X0A tuples
Tuples are generally represented in parentheses.
Compared with the list, the list can be modified, but the list cannot be modified.
>>> Username = ('H', 'e', 'L', 'L', 'O', ',', 'W', 'O ', 'R', 'L', 'D ')
>>> Password = ('X',) # Create a tuple containing only one data