A transformation instance of two-dimensional array in Python

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.