text="we are in pakistan and love pakistan and olx"
dict1={"pakistan": "COUNTRY", "olx": "ORG"}
I need to match the key in the text, if it exists store the indices of that word as keys in new dictionary where as the value should be the same as in dict1 for that particular word, for eg the output should be like this:
dict2={[10:17]:"COUNTRY",[27:34]:"COUNTRY","[40:42]:"ORG"}
First, I have to address that your expected outcome which is having lists, which are not hashable, as the key of a dictionary is not possible. See https://wiki.python.org/moin/DictionaryKeys
The way to produce something similar is to use the re library:
import re
text="we are in pakistan and love pakistan and olx"
dict1={"pakistan": "COUNTRY", "olx": "ORG"}
dict2 = {}
for key, value in dict1.items():
matched_all = re.finditer(key,text)
for matched in matched_all:
dict2[matched.span()] = value
print(dict2)
This will give you:
{(10, 18): 'COUNTRY', (28, 36): 'COUNTRY', (41, 44): 'ORG'}