0

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?

2 Answers 2

7

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Probably worth pointing out, another name for the abstract data type "dictionary" implemented by dict is called often called a map. Indeed, in Python, a dict is a mapping type
I'll note that this is especially performant because dict has a lookup of O(1) instead of O(n), so overall your complexity is O(n), rather than O(n*m)
0

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]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.