1 Convert list to array
If the array of arrays for the list is not structured, such as
A = [[up], [3,4,5]]
After a = Numpy.array (a)
The type of a is ndarray, but the element in a a[i] is still a list.
If a = [[up], [3,4]]
After a = Numpy.array (a)
The type of a is Ndarray, the element inside A[i] is also ndarray
2 Flatten function
Python itself does not have a flatten function, and the array in NumPy has a flatten function.
As with 1, if a is not regular, then the flatten function fails
You can write a function yourself
def Flat (List_tree): = [] for in list_tree: if isinstance (i, list): Res.extend (flat (i)) elif isinstance (i, Np.ndarray): res.extend (Flat (I.tolist ())) Else : res.append (i) return Res
3 parallel traversal of two arrays
The built-in zip function lets us use a for loop to use multiple sequences in parallel. In the basic operation,thezip obtains one or more sequences as parameters, then returns a list of tuples, matching the elements in those sequences into pairs.
Example one:
L1 = [1,2,3,4]
L2 = [5,6,7,8]
To merge the elements in these lists, you can use a zip to create a list of tuple pairs. Like range ,zip is an iterative object, so you must include it in a list All results are displayed on one side of the call.
Zip (L1,L2)
List (Zip (L1,L2))! comment out this line, the result of the operation is still as follows
for (x, y) in Zip (L1,L2):
Print (x, ' + ', y, ' = ', x + y)
Some problems with Python numpy array