Some problems with Python numpy array

Source: Internet
Author: User

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

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.