0

I have a subset of strings and want to find its indices in the set which contain all strings, like

subset = ['a','b','d']

and the set of strings is['a','b','c','d','e','f','g'].

The function should take input the subset and set of strings and return indices of subset, [0,1,3].

I tried using a for loop, it is taking a lot of time. Is there a python function of any library which can do this task.

Avishka Dambawinna
  • 1,180
  • 1
  • 13
  • 29

1 Answers1

0

Does this solve your question?
Even on a large list it can perform moderately fast.(ie. On a list of length 1 million can perform calculations in about 0.6 seconds depending on your computer my computer is pretty moderate)

import numpy as np

lookup = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
subset = ['a', 'b', 'd']

indexes = list(np.searchsorted(lookup, subset))
print(indexes)

If you any other queries regarding this answer, please let me know!
Happy Coding!

Aryan
  • 1,093
  • 9
  • 22
  • In stack overflow the way to say thanks is to upvote the answers :-) helps the contributor alot – Aryan Dec 12 '20 at 05:18
  • Hi Aryan, np.searchsorted is not working in some cases, suppose if the element in substring is not present in the main string, it is not returning the NA instead its return index where it can be inserted to maintain sorted list. – Anooj Gandham Dec 30 '20 at 06:28