How to retrieve multiple arrays using slice in python.

It is a memorandum. It's easy to forget how to retrieve multiple arrays with slice in python, so I'll leave it for you.

Make an array with numpy

arr2d = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])

This will create the following multiple array: ([[ 5, 10, 15], [20, 25, 30], [35, 40, 45]])

From this, the upper right [10, 15] [25, 30] I would like to take out.

I think there are various methods, but

arr2d[:2, 1:]

You can take it out with. Explained below.

First specify the line

arr2d[:2]

Where is the part that can be taken out with? This means that the row of arr2d is fetched up to one before index 2.

In other words ([[ 5, 10, 15], [20, 25, 30]])

Is taken out.

Then specify the column

Then take the column from the above matrix.

The completed form is

arr2d[:2, 1:]

However, the point to pay attention to here is [1:].

If you omit the ending index, you can retrieve from index 1 to the end. Since the column is specified here, it is retrieved

([[10, 15], [25, 30]])

It will be.

Summary

In a multidimensional array array, array [n, m] is an image of first specifying a row with n and then narrowing it down with m !!

If the start index is omitted, the slice is extracted from the beginning to before the end index.

On the other hand, remember that if you omit the end index, the start index to the end will be extracted.

that's all.

Recommended Posts

How to retrieve multiple arrays using slice in python.
How to slice a block multiple array from a multiple array in Python
How to develop in Python
How to write string concatenation in multiple lines in Python
How to exit when using Python in Terminal (Mac)
How to execute a command using subprocess in Python
How to take multiple arguments when doing parallel processing using multiprocessing in python
How to install python using anaconda
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to define multiple variables in a python for statement
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
How to do R chartr () in Python
Send email to multiple recipients in Python (Python 3)
How to work with BigQuery in Python
Log in to Slack using requests in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to delete multiple specified positions (indexes) in a Python list
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to generate a new loggroup in CloudWatch using python within Lambda
How to get a value from a parameter store in lambda (using python)
How to plot galaxy visible light data using OpenNGC database in python
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
How to write a string when there are multiple lines in python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
[Python] How to draw multiple graphs with Matplotlib
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
How to plot autocorrelation and partial autocorrelation in python