Reputation: 197
I have 2 list below
Token_Sentence=[['This','is','a','book'],['This','is','a','cat'],['Those','are','two','books']]
Mapping=[['This',1],['is',2],['a',3],['book',4],['cat',5],['Those',6],['are',7],['two',8],['books',9]]
And I want to map Token_Sentence (convert text to index number) like this
[[1,2,3,4],[1,2,3,5],[6,7,8,9]]
and here is my code
for a in range(len(Token_Sentence)):
for b in range(len(Token_Sentence[a])):
for c in range(len(Mapping)):
if Token_Sentence[a][b]==Mapping[c][0]:
Token_Sentence[a][b]=Mapping[c][1]
But the Problem is it's take a very long time to run (my real data list is pretty large).
Is there are other ways to achieve my goal that faster and simpler than I did?
Upvotes: 0
Views: 994
Reputation: 12669
Above Answer is good , Just wanted to show if you want without converting to dict :
Token_Sentence=[['This','is','a','book'],['This','is','a','cat'],['Those','are','two','books']]
Mapping=[['This',1],['is',2],['a',3],['book',4],['cat',5],['Those',6],['are',7],['two',8],['books',9]]
print([[k[1] for j in i for k in Mapping if j==k[0]] for i in Token_Sentence ])
output:
[[1, 2, 3, 4], [1, 2, 3, 5], [6, 7, 8, 9]]
Upvotes: 0
Reputation: 71451
You can create a mapping from Mapping
:
Token_Sentence=[['This','is','a','book'],['This','is','a','cat'],['Those','are','two','books']]
Mapping=[['This',1],['is',2],['a',3],['book',4],['cat',5],['Those',6],['are',7],['two',8],['books',9]]
d = dict(Mapping)
new_sentence = [[d[b] for b in i] for i in Token_Sentence]
Output:
[[1, 2, 3, 4], [1, 2, 3, 5], [6, 7, 8, 9]
Upvotes: 7