1. List of lists
1.1 Slices
# define a list.
list = [1, 2, 3, 4, 5]
Reads characters from left to right (the default step is 1). Such as:
LIST[-2:-1] # Returns a list data type, [6]
LIST[2] # returns an int data type, 3
Reads the string from right to left (the default step is 1). Such as:
LIST[-1:-2] # Returns an empty list, []
LIST[-1:-2:-1] # Step IS-1, [5]
1.2 Built-in list method
such as: a = "123"
List (a)
# Returns a list of parameters that can iterate over an object. The contents of the output keep the elements and order of the incoming iterated objects. If the argument is empty, an empty list is returned.
1.3 The specific differences between xrange and range
Usage of 1.3.1 Xrange (used only in Python2):
Xrange (Start, end, step)
Xrange it generates a Xrange object.
1.3.2 Comparison
Range: Generates a list object directly. # (in Python3, range is to generate a Range object that supports slicing)
Xrange: Generates a Xrange object. Operation data is large, only a single element is returned each time, saving memory.
The application of 1.3.3 list deduction formula
1) Remove the square of all values of 1-10.
[X*x for X in range (10)] # results: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
2) generates a string.
[' num%s '% i for I in range (10)]
# results: [' num 0 ', ' num 1 ', ' num 2 ', ' num 3 ', ' num 4 ', ' num 5 ', ' num 6 ', ' num 7 ', ' num 8 ', ' num 9 ']
3) Generate tuples.
[(x, Y) for x in range (2) for Y in range (2)]
# double Iteration list, in the form of tuples (x, y), generates a list with the result: [(0, 0), (0, 1), (1, 0), (1, 1)]
4) generate a dictionary.
Dict ([(x, Y) for x in range (4) for Y in range (3)])
# The iteration of the first list is used as the dictionary key, and the second list iterates as the value of the dictionary. The result is:
{0:2, 1:2, 2:2, 3:2}
3. Set Set
The collection has no order and cannot be manipulated with slices and indexes.
3.1 Create a collection.
Set (): Variable
Frozenset (): Immutable (cannot perform additions, deletions, etc.)
3.2 Add operation. Such as:
A = Set ("ASD") # Result: {' A ', ' d ', ' s '}
Add:
A.add ("Panisme") #按原始字符串传入. Result: {' panisme ', ' s ', ' d ', ' a '}
Update:
A.update ("Panisme") #将字符串分割传入.
#结果: {' m ', ' e ', ' P ', ' panisme ', ' s ', ' n ', ' Boy ', ' I ', ' d ', ' a '}
3.3 Delete
Remove:
A.remove ("Panisme") # Result: {' m ', ' e ', ' P ', ' s ', ' n ', ' Boy ', ' I ', ' d ', ' a '}
3.4 Judging Member relationships
In: ' A ' in a # determines whether the character "a" is inside set a, and returns a bool value.
Not in
3.5 intersection, and set, difference set
Intersection:&
The Set: |
Difference set:-
3.6 Set de-weight
Remove list content elements to repeat, such as:
b = [1,23,4,5,2,2,1,2]
Set (b) # Returns the result with a set of weight: {1, 2, 4, 5, 23}
List (set (b)) # to go to the list for the purpose of getting to the table: [1, 2, 4, 5, 23]
...
Initial Python (ii)