Alex Nikitin
Alex Nikitin

Reputation: 524

Building chains from nested list

I have a nested list:

[['мама', 'мыть', '10', 'рама'],
 ['мыть', 'рама', '5', 'долго'],
 ['мама', 'мыть', '10', 'рама'],
 ['мыть', 'рама', '3', 'вчера'],
 ['мыть', 'рама', '10', 'вчера'],
 ['рама', 'вчера', '1', 'поздно']]

What I need is to build chains where last two non-digital string elements of one list are equal to first two non-digital string elements of another list, for example in:

['Мама', 'мыть', '10', 'рама']

and

['мыть', 'рама', '5', 'долго']

'мыть', 'рама' are a match, so the final output should be:

[['мама', 'мыть', '10', 'рама', '5', 'долго'],
 ['мама', 'мыть', '10', 'рама', '3', 'вчера'],
 ['мама', 'мыть'  '10', 'рама', '3', 'вчера', '1', 'поздно']]

Digits are kind of probability and should be left as is. I think there should be some kind of iterative search, but I am not sure. Any help would be appreciated.

Upvotes: 2

Views: 71

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134005

1 - Create a dictionary from your list, with the key being the first two words, combined. Something like:

key: 'Мама_мыть' value: ['Мама', 'мыть', '10', 'рама'],
key: 'мыть_рама' value: ['мыть', 'рама', '5', 'долго'],
   ... etc ...

2 - Iterate over your list, creating a key from the last two non-numeric values in each entry. And look that value up in the dictionary.

3 - When you find a match, create the output.

Upvotes: 1

Related Questions