1. Deep linked list
First, we will introduce some common methods for linked lists:
Append (x): adds an element to the end of the linked list.
Extend (l): merges another linked list into this linked list.
Insert (I, X): insert an element before the specified position
Remove (x): Delete the first element with the value of X in the linked list. If this element does not exist, an error is returned.
Pop ([I]): deletes the element whose index is I in the linked list and returns it. If index I is greater than the maximum index of the linked list, an error is returned. If no index is provided, delete and return the last element.
Index (x): returns the index of the first X element in the linked list.
Count (x): returns the number of times X appears in the linked list.
Sort (): sort the elements in the linked list in the local place # The local meaning is in ruby! Function at the end
Reverse (): reverse local sorting
1. How to use a linked list as a stack
Stack features advanced post-release, append () press-in, pop () pop-up
2. How to Use the linked list as a queue
The queue is characterized by first-in-first-out, append () is used to route an object to the end of the team, and pop (0) is used to pull the first object
3. Functional programming tools
For linked lists, three functions are very useful:
(1) filter (function, list)
This function is a filter function that returns all element linked lists in the list that return true after the function operation.
For example:
1 Ls = [ 1 , 2 , 3 , 4 ]
2 Ls2 = Filter ( Lambda X: x > 1 , Ls)
3 Print Ls # [1, 2, 3, 4]
4 Print Ls2 # [2, 3, 4]
Note: The filter function is not a local processing function, but returns the execution result.
(2) map (function, list)
This function processes the functions in sequence, and uses the function to calculate the elements in the list in sequence. The calculation results are returned in a linked list.
(3) Reduce (function, list)
1 Reduce ( Lambda X, Y: x + Y, range ( 1 , 5 )) # Returns 10 because [1, 2, 4] 1 + 2 = 3 3 + 3 = 6 6 + 4 = 10
2
3
This function is rarely used and can be ignored.
4. chain table Derivation
Is to suffix the if or for statement in an expression or function, and then export a linked list, such:
1 [X * 2 For X In Range ( 10 )] # [,]
2 [ " Lee: " + S For S In [ ' Hi ' , ' Hallo ' ] # ["Lee: Hi", "Lee: Hallo"]
3 [( Lambda X: x * 2 ) (X) For X In Range ( 10 )] # The result is the same as that in the first sentence. However, the first sentence uses the expression and the function.
Ii. Del statement
The function is to delete the specified list element. You can delete the element of the specified part.
1 A = Range ( 5 )
2 Del A [0] # [1, 2, 3, 4]
3 Del A [ 1 , 3 ] # [1, 4]
4 Del A # Delete variable
3. tuples and Sequences
Tuples are also a composite data structure. Multiple elements can be organized together, but the values of one element cannot be changed.
Tuples are enclosed.
Use x = () directly when creating empty tuples.
When constructing a single-element tuples, use x = 1 or X = (1.
Iv. Sets
The main functions of sets are:
You can perform "and operations" on the list to obtain the elements of the two lists.
You can perform "or operations" on the list to obtain all the elements of the two lists.
You can perform an exclusive or operation on the list to obtain the elements that the two lists do not have.
You can perform a subtraction operation on the list to exclude the elements of the other list from one list.
To obtain a set, use the Set () function. For example:
1 S1 = Set (range (0, 5 )) # ([0, 1, 2, 3, 4])
2 S2 = Set (range ( 3 , 8 )) # ([3, 4, 5, 6, 7])
3 S1 - S2 # ([0, 1, 2])
4 S1 | S2 # ([0, 1, 2, 3, 4, 5, 6, 7])
5 S1 & S2 # ([3, 4])
6 S1 ^ S2 # ([0, 1, 2, 5, 6, 7])
5. Dictionary
The dictionary is a hash table, which is saved by key values. The key must be unchangeable. It is generally a string or number, or a tuple that only contains strings or numbers, if the tuples contain variable objects, they cannot be used as keys.
1 A = ( 1 ,)
2 Hash = {A:}
3 Hash [A] = 1 # OK
4 A = 1 # OK
5 Print Hash # {(1,): 1}
You can use del to delete a key/value. If you assign a value to an existing key, it overwrites the key value.
You can use the keys () method to return the list of keys,
You can use the has_key () method to check whether a key exists.
Vi. Recycling Technology
During dictionary loops, you can use the iteritems () method to read the keywords and corresponding values at the same time.
1 H = { 1 : 1 , 2 : 2 }
2 For K, V In H. iteritems ():
3 Print K, V
4
5
when looping in a sequence, the index position and corresponding values can be read simultaneously using the enumerate () function.
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 for I, v in enumerate ([ ' A ' , ' B ' , ' C ' ])
2 Print I, v
3
4
Loop multiple sequences at the same time, you can use the ZIP () overall interpretation.
1 A = [ 1 , 2 , 3 ]
2 B = [ 2 , 4 , 6 ]
3 For V1, V2 In Zip (A, B ):
4 Print V1, V2
A reverse loop sequence is required. A forward position sequence is required before the reversed () function is called.
1 For I In Reversed (range ( 1 , 100 ))
2 Print I
3
4
You can use sorted () to sort the sorted sequence loops first.
1 For I In Sorted ([ 1 , 3 , 2 , 5 , 9 , 7 ])
2 Print I
3
4