Finding position of each word in a sub-array of a multidimensional array
Posted
by Shreyas Satish
on Stack Overflow
See other posts from Stack Overflow
or by Shreyas Satish
Published on 2010-04-20T10:06:25Z
Indexed on
2010/04/20
13:33 UTC
Read the original article
Hit count: 223
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!
© Stack Overflow or respective owner