[PYTHON] How to return multiple indexes with index method

How to return multiple indexes with index method

There is an index method in python. This method returns the index that hits the first search character, but it seems that it cannot return all indexes. Even if I search it, it doesn't come out for some reason (although the search method may be wrong). So I briefly wrote an extended version of the index method that returns multiple indexes that hit the string I searched for.

index_multi.py


def index_Multi(List,liter):
	#List is the list body, liter is the character you want to search
	index_L = []
	for val in range(0,len(List)):
		if liter == List[val]:
			index_L.append(val)
	return index_L

If you give List as the list group you want to search and liter as the character string you want to search, the index that matches the character string you want to search will be returned.

test

Extract 500 indexes from the elements contained in test_L.

index_test.py


def index_Multi(List,liter):
	#List is the list body, liter is the character you want to search
	index_L = []
	for val in range(0,len(List)):
		if liter == List[val]:
			index_L.append(val)
	return index_L

if __name__ == "__main__":

	test_L = [500,0,0,0,0,0,0,500,200]
	print index_Multi(test_L,500)

Execution result


[0, 7]

If there is a smart way or method, we will delete this article.

Smart way

** Addition ** You pointed out. This method can be expressed in one line.

index_test.py



if __name__ == "__main__":

	test_L = [500,0,0,0,0,0,0,500,200]
    index_num = [n for n, v in enumerate(test_L) if v == 500]
	print index_num

reference

Element confirmation in operator, index method, count method

Recommended Posts

How to return multiple indexes with index method
How to title multiple figures with matplotlib
[Python] How to draw multiple graphs with Matplotlib
How to use return
Summary of how to share state with multiple functions
Method chain with `return self`
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to embed multiple embeds in one message with Discord.py
How to return to the previous directory with the Bash cd command
Get Python list elements with multiple indexes (number) Simple method
How to extract other than a specific index with Numpy
Python: How to use async with
Connect to multiple databases with SQLAlchemy
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
How to get started with Scrapy
How to get started with Python
How to deal with DistributionNotFound errors
How to get started with Django
How to use FTP with Python
How to calculate date with python
How to install mysql-connector with pip3
How to install Anaconda with pyenv
How to authenticate with Django Part 3
How to deal with "You have multiple authentication backends configured ..." (Django)
How to delete multiple specified positions (indexes) in a Python list
How to do arithmetic with Django template
How to get parent id with sqlalchemy
How to add a package with PyCharm
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to utilize multi-core from multiple languages
How to prevent package updates with apt
How to work with BigQuery in Python
How to use Ass / Alembic with HtoA
How to deal with enum compatibility errors
How to use Japanese with NLTK plot
How to do portmanteau test with python
How to search Google Drive with Google Colaboratory
How to reassign index in pandas dataframe
How to display python Japanese with lolipop
How to draw a bar graph that summarizes multiple series with matplotlib
How to download youtube videos with youtube-dl
How to use jupyter notebook with ABCI
How to power off Linux with Ultra96-V2
"How to pass PATH" to learn with homebrew
How to use CUT command (with sample)
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to read problem data with paiza
How to use SQLAlchemy / Connect with aiomysql
How to get started with laravel (Linux)
How to group volumes together with LVM
How to install python3 with docker centos