I have quite a large dataset in the form of a list of lists. Basically, these list of lists are tokenized sentences (Each row is a sentence and each sentence is tokenized).
train_dataset - The 2D array (or list of lists) contain around 150000 sentences/rows and each sentence roughly contains 12 to 15 words.
td - Another 1D list contains 55000 UNIQUE words. If any word from these words are present in the 2D array, I need to replace them with .
I tried below ways to do so but they take a very long time, even in Google Colab's different runtime environments:
train_dataset = ['<UNK>' if train_dataset[i][j] in td else train_dataset[i][j] for i in range(0,len(train_dataset)) for j in range(0,len(train_dataset[i])) ]
train_dataset
I also tried to convert the list to NUMPY arrays and try so. Still no luck.
import numpy as np
np_tr = np.array(train_dataset)
for st in td:
for i in range(0,len(np_tr)):
for j in range(0,len(np_tr[i])):
if np_tr[i][j] == st:
np_tr[i][j] = '<UNK>'
I want to know if there is any efficient way to do this in Python. I don't have access to PySpark or Hadoop to achieve this.
Thanks in advance!