Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple

You can do it like this.

itemgetter_iterable_example.py


import numpy as np
from operator import itemgetter

np_list = np.linspace(0, 1, num=11)
# -> array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ])
py_list = list(np_list)
indexes = [1, 3, 5]  #Appropriate index

#With NumPy it looks like this
np_list[indexes]
# -> array([ 0.1,  0.3,  0.5])

#A Python list looks like this
itemgetter(*indexes)(py_list)
# -> (0.1, 0.3, 0.5)

There is also a story that you only have to convert it to NumPy once.

Recommended Posts

Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
Group by consecutive elements of a list in Python
Get the number of specific elements in a python list
Display a list of alphabets in Python 3
Extract every n elements from an array (list) in Python and Ruby
Make a copy of the list in Python
View drug reviews using a list in Python
Randomly select elements from list (array) in python
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
[Python] Outputs all combinations of elements in the list
Get the value of a specific key in a list from the dictionary type in the list with Python
How to check in Python if one of the elements of a list is in another list
Getting list elements in Python
Python: Create a dictionary from a list of keys and values
Extract array elements and indexes in descending order with numpy
Extract the value closest to a value from a Python list element
How to create an instance of a particular class from dict using __new__ () in python
[python] Manage functions in a list
python / Make a dict from a list.
Extract text from images in Python
Delete multiple elements in python list
Extract strings from files in Python
Try to get a list of breaking news threads in Python.
[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)
How to delete multiple specified positions (indexes) in a Python list
Get a list of files in a folder with python without a path
Getting a combination of elements using itertools
(Bad) practice of using this in Python
Get a list of packages installed in your current environment with python
[Python] How to put any number of standard inputs in a list
# 5 [python3] Extract characters from a character string
How to get a value from a parameter store in lambda (using python)
Scraping a website using JavaScript in Python
Download images from URL list in Python
[python] Get a list of instance variables
Receive a list of the results of parallel processing in Python with starmap
Basic grammar of Python3 series (list, tuple)
Draw a tree in Python 3 using graphviz
Summary of built-in methods in Python list
Generate a class from a string in Python
How to format a list of dictionaries (or instances) well in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
[Python] Get a list of folders only
Meaning of using DI framework in Python
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
How to pass the execution result of a shell command in a list in Python
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
Perform "diagonalization of symmetric matrix A using orthogonal matrix U" in Python (eigenvalue decomposition)
How to get a list of files in the same directory with python
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
[python] Check the elements of the list all, any
[Python] Sort the list of pathlib.Path in natural sort
How to clear tuples in a list (Python)
[python] Create a list of various character types
Get the caller of a function in Python
Basic operation list of Python3 list, tuple, dictionary, set
Get only the subclass elements in a list
Make a joyplot-like plot of R in python
Call a Python script from Embedded Python in C ++ / C ++