Finding position of each word in a sub-array of a multidimensional array
- by Shreyas Satish
I have an array:
tokens = [["hello","world"],["hello","ruby"]]
all_tokens = tokens.flatten.uniq # all_tokens=["hello","world","ruby"]
Now I need to create two arrays corresponding to all_tokens, where the first array will contain the position of each word in sub-array of tokens. I.E Output:
[[0,0],[1],[1]] # (w.r.t all_tokens)
To make it clear it reads, The index of "hello" is 0 and 0 in the 2 sub-arrays of tokens.
And second array contains index of each word w.r.t tokens.I.E Output:
[[0,1],[0],[1]]
To make it clear it reads,the index of hello 0,1. I.E "hello" is in index 0 and 1 of tokens array.
Cheers!