In this paper, we describe the transformation method of two-dimensional array in Python. Share to everyone for your reference. Here's how:
Let's look at the following code:
arr = [[1, 2, 3], [4, 5, 6], [7, 8,9], [10, 11, 12]]
The results of the operation are as follows:
[[1, 4, 7, ten], [2, 5, 8, one], [3, 6, 9, 12]]_-------------------------------------------------[[1, 4, 7, 10], [2, 5, 8, 1 1], [3, 6, 9, 12]]
Here's an explanation:
1. First method : Map (list, zip (*arr))
Zip ([iterable, ...])
This function returns a list of tuples, where the i-th tuple contains thei-th element from each of the argument sequences or Iterables.
Zip ()
This function returns a list of tuples, where the I-tuple contains the element I-elements of each parameter of the queue passed in from the parameter.
Let me give you an example:
>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = Zip (x, y) >>> zipped[(1, 4), (2, 5), (3, 6)]
The fact that zip (*arr) returns is [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)], except that each of its elements is a tuple
Map (func, list):
Invokes the Func method on each element in the list, returning
The parameter *arr is the syntax used by Python to pass arbitrary location-based arguments
2. Second method: [[[R[col] for R with Arr] for Col in range (len (arr[0))]
The inner derivation changes the element selected (from the line), and the outer derivation affects the selector (that is, the column)
Hopefully this article will help you with Python programming.