NumPy implementation of extended methods for merging multidimensional matrices and lists

Source: Internet
Author: User
This article mainly introduces the NumPy implementation of the integration of multidimensional matrix, List extension method, has a certain reference value, now share to everyone, there is a need for friends can refer to

First, merging multiple numpy matrices

1. Create two multidimensional matrices first

The size of matrix A is (2,3,2)

The size of Matrix B is (3,2,3)

Using the Concatentate function, you can combine two multidimensional matrices

After merging should be (5,3,2)

In [1]: Import numpy as Npin [2]: A = Np.ndarray ((3, 2, 3)) in [3]: b = Np.ndarray ((2, 2, 3)) in [4]: Print (A.shape, b.shape ) (3, 2, 3) (2, 2, 3) in [5]: c = np.concatenate ((A, b), Axis = 0) in [6]: Print (C.shape) (5, 2, 3) in [7]:

Second, the addition of matrices

The addition of matrices is a function of append, and list has this function, but they are used in slightly different ways.

1. Create a Ndarray

2. Then append using the Np.append () function (note that np.append, not a.append)

In [2]: Import NumPy as Npin [3]: a = Np.array ([1, 2, 3, 4, 5]) in [4]: a = Np.append (A, ten) in [5]: Aout[5]: Array ([1, 2, 3, 4, 5,]) in [6]: a = Np.append (A, [1, 2, 3]) in [7]: Aout[7]: Array ([1, 2, 3, 4, 5, 10, 1, 2, 3])

Third, the extension of the list (extend)

1, the extension of the list is to merge two lists

2, using Extend function

In [9]: a = [1, 2, 3, 4]in [ten]: b = [5, 6, 7, 8]in [all]: aout[11]: [1, 2, 3, 4]in []: bout[12]: [5, 6, 7, 8]in []: C = A.extend (b) in [+]: cIn []: aout[15]: [1, 2, 3, 4, 5, 6, 7, 8]

Note that the return value of the Extend function is none, so the output of line 13th C above is empty, and A's value has changed, so it is expanded directly after a, and there is no return value.

Iv. Additions to the list

Append the list directly with append.

1. Create List A

2. Append data to the back of a

in [+]: a = [1, 2,3,4]in []: A.append (6) in [+]: aout[30]: [1, 2, 3, 4, 6]in [31]:

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.