Examples of two-dimensional array transformation in python and python Array

Source: Internet
Author: User

Examples of two-dimensional array transformation in python and python Array

This article describes how to transform a two-dimensional array in python. Share it with you for your reference. The specific method is as follows:

First look at the following code:

arr = [ [1, 2, 3], [4, 5, 6], [7, 8,9], [10, 11, 12]]  print map(list, zip(*arr)) print '_-------------------------------------------------' print [[r[col] for r in arr] for col in range(len(arr[0]))] 

The running result is as follows:

[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]_-------------------------------------------------[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]

Here is an explanation:

1. method 1: 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. the I-th tuple contains the I-th element of each parameter element of the queue passed in from the parameter.

Here is another example:

>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = zip(x, y)>>> zipped[(1, 4), (2, 5), (3, 6)]

Actually, zip (* arr) Returns [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)], however, each of its elements is a tuples.

Map (func, list ):

Call the func method for each element in the list and return the list
Parameter * arr is the syntax used by python to pass any location-based parameter.

2. Method 2:[[R [col] for r in arr] for col in range (len (arr [0])]
The inner layer derivation changes the selected elements (from the row), while the outer layer derivation affects the selection child (that is, the column)

I hope this article will help you with Python programming.


What is the difference between the instance features in UG and the array in the transformation command?

The main difference is relevance .. There are also parameter features. Strength features have parameters (associations) and no parameters during transformation.

What are the application scenarios of ug rectangular arrays? Why can't I use a single object array? The array will show that the tool body is completely in the target external body?

Let me tell you! Do you see what is written in the upper-left corner of UG? That's the feature! You have a hole in a large object, and then the array is ready. You need to use ctrl + T to array the object. Hope you understand the essence

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.