[PYTHON] Extract the index of the original set list that corresponds to the list of subsets.

Prepare two lists (A, B). Here is $ B \ subset A $. I want to retrieve the index of A that corresponds to the subset B.

In other words

A = [1\List of integers in sim 100]\\
B = [1\Even list of sim 100]\\

\Longrightarrow index = [1, 3, 5, 7..., 99]

Get an index like this.

Implementation

A = list(range(1, 101))

B = list(range(2, 102, 2))

Numpy has a function that returns True for the element of A that corresponds to the subset B.

import numpy as np

isin = np.isin(A, B)
print(isin)
#array([False,  True, False,  True, False,  True, False,  True, False...
print(len(isin) == len(A))
#True

There was also a function in Numpy that returned the index of the True element.

index = list(np.where(isin))[0]
print(index)
#array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33...

I was able to implement it.

Practice

Prepare A given a random number and B obtained from it at random.


A = random.sample(range(1, 100), k=5)
B = random.sample(A, 3)

print(A)
#[37, 24, 76, 55, 52]
print(B)
#[55, 76, 37]

index = list(np.where(np.isin(A, B)))[0]
print(index)
#array([0, 2, 3])

I was able to get it randomly.

from now on

I was able to get the index, but it was sorted. In other words, the index that considers the order of B cannot be obtained as the first element of B is the number element of A. You can check each one with the for statement, but in the case of large lengths A and B, the amount of calculation becomes very large. So you will have to think of another way.

Recommended Posts

Extract the index of the original set list that corresponds to the list of subsets.
I tried to get the index of the list using the enumerate function
[Python] A program that rotates the contents of the list to the left
I measured 6 methods to get the index of the maximum value (minimum value) of the list
Set the range of active strips to the preview range
Convert a slice object to a list of index numbers
[Blender] How to dynamically set the selection of EnumProperty
Set the specified column of QTableWidget to ReadOnly StyledItemDelegate
How to extract conditions (acquire all elements of Group that satisfy the conditions) for Group by Group
Get the value of a specific key up to the specified index in the dictionary list in Python
Try to get the function list of Python> os package
Try to extract the keywords that are popular in COTOHA
How to set the extended iso8601 format date to the Dataframe index
Extract the value of dict or list as a string
The story that the private key is set to 600 with chmod
[Python3] List of sites that I referred to when I started Python
How to connect the contents of a list into a string
Extract the value closest to a value from a Python list element
Try to extract the features of the sensor data with CNN
Extract the Azure service list
The story that the version of python 3.7.7 was not adapted to Heroku
Set the last modified date of the child file to the modified date of the parent directory
A story that struggled to handle the Python package of PocketSphinx
Find all patterns to extract a specific number from the set
How to set up the development environment of ev3dev [Windows version]
[python] A note that started to understand the behavior of matplotlib.pyplot
[For beginners] I want to get the index of an element that satisfies a certain conditional expression
[Python] Three methods to compare the list of one-dimensional array and the list of two-dimensional array and extract only the matching values [json]
About the basics list of Python basics
Supplement to the explanation of vscode
Preparing to load the original dataset
Sort the list that contains the dict
Heroku deployment of the first Django app that beginners are addicted to
[Python] A program that calculates the number of socks to be paired
Python> sys.path> List of strings indicating the path to search for modules
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Linux] Command to get a list of commands executed in the past
I want to sort a list in the order of other lists
I tried to automatically extract the movements of PES players with software
[Python] How to use the enumerate function (extract the index number and element)
I tried to extract and illustrate the stage of the story using COTOHA
How to create a wrapper that preserves the signature of the function to wrap
To speed up python, summarize the amount of calculation of collection type (list / tuple / dictionary / set) for each purpose.