Skip to content Skip to sidebar Skip to footer

How To Find In A Parent String List, The Indexes Corresponding To A Child String List

I am writing a code which reads data from a text file. I load the data using numpy loadtxt and it could look like something like this: import numpy as np Shop_Products = np.array

Solution 1:

np.searchsorted can take a sorting permutation as an optional argument:

>>>sorter = np.argsort(Shop_Products)>>>sorter[np.searchsorted(Shop_Products, Shop_Query, sorter=sorter)]
array([4, 1])
>>>Shop_Inventory[sorter[np.searchsorted(Shop_Products, Shop_Query, sorter=sorter)]]
array([8, 6])

This is probably faster than np.in1d, which also needs to sort the array. It also returns values in the same order as they come up in Shop_Query, while np.1d will return the values in the order they come up in Shop_Products, regardless of the ordering in the query:

>>> np.in1d(Shop_Products, ['Cheese', 'Bread']).nonzero()
(array([1, 4]),)
>>> np.in1d(Shop_Products, ['Bread', 'Cheese']).nonzero()
(array([1, 4]),)

Solution 2:

You can use in1d() and nonzero() to find the indices of the items in Shop_Products:

>>> np.in1d(Shop_Products, Shop_Query).nonzero()
(array([1, 4]),)

(in1d returns a boolean array indicating whether an item is in the second list, nonzero returns the indices of the True values.)

To look up the corresponding values in Shop_Inventory, use this result to index the array:

>>>i = np.in1d(Shop_Products, Shop_Query).nonzero()>>>Shop_Inventory[i]
array([6, 8])

Post a Comment for "How To Find In A Parent String List, The Indexes Corresponding To A Child String List"