Specify multiple list indexes (Python)

If you want to retrieve multiple values from a list in Python, you can retrieve them in slices if they are contiguous, but I didn't know how to retrieve them if they were not contiguous, so I looked it up.

Verification environment

Method

How to use itemgetter

It seems that ʻitemgetter` can be used from python2.5 or later, so I will try using it. If you pass an index in the list, the value of that index will be returned.

from operator import itemgetter

a = ("a", "b", "c", "d", "e")

print itemgetter(1,3)(a) # ('b', 'd')

The reason why I wanted to do this is that it would be nice if the label was returned by machine learning, but when extracting the original data from it, I couldn't think of an easy way to write it. For example, in a classifier that distinguishes between lowercase and uppercase letters, if a label is returned and only uppercase letters are extracted from it, I wrote as follows.

from operator import itemgetter
import numpy

label_prediction = numpy.array([0,0,1,1,0,1])
data = ["a","b","C","D","e","F"]

label_match = numpy.where(label_prediction==1)[0]
print itemgetter(*label_match)(data)

Please let me know if there is another good way.

How to use numpy

I learned from s-wakaba! Thank you very much. I was pessimistic that I couldn't use numpy if it was a character string, but I could use it normally. Much more beautiful than using itemgetter.

import numpy

label_prediction = numpy.array([0,0,1,1,0,1])
data = ["a","b","C","D","e","F"]

print numpy.array(data)[label_prediction==1]

It would be helpful if you could tell me a better way to write down what you researched. I would like to take notes and expose my ignorance.

Recommended Posts

Specify multiple list indexes (Python)
[Python] list
Delete multiple elements in python list
Get Python list elements with multiple indexes (number) Simple method
Python basics: list
Python> Comprehension / Comprehension> List comprehension
How to delete multiple specified positions (indexes) in a Python list
Python list manipulation
Sorted list in Python
Python Exercise 2 --List Comprehension
Python> list> extend () or + =
Copy of multiple List
[Python] Create multiple directories
Python list comprehension speed
Filter List in Python
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
Python exception class list
Initialize list with python
Multiple regression expressions in Python
Python hand play (two-dimensional list)
Summary of Python3 list operations
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
Python Basic Course (5 List Tuples)
Install multiple versions of Python
Python list is not a list
Specify options when running python
Decompress multiple compressed files (Python)
[Python] Copy of multidimensional list
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Paiza Python Primer 4: List Basics
Avoid multiple loops in Python
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
Specify python version with virtualenv
Python #list for super beginners
Getting list elements in Python
Prohibit multiple launches in python
[ev3dev × Python] Control of multiple motors
[Python] Show multiple windows in Tkinter
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
[Python] Sort iterable by multiple conditions
python memo --Specify options with getopt
Convert list to DataFrame with python
[Python beginner] Divide one list (5 lines).
python / Make a dict from a list.
Python> list> Convert double list to single list
Post multiple Twitter images with python
Animate multiple still images with Python
[Python] How to use list 3 Added
Statistical test (multiple test) in Python: scikit_posthocs
Write python list fast vim tips
Multiple integrals with Python and Sympy
Python3> List generation from iterable> list (range (5))