T
T
Timebird2018-10-25 09:22:16
Python
Timebird, 2018-10-25 09:22:16

How to find neighboring elements of a dictionary by keys?

There is a dictionary:

{'слово': 'S', 'адвокат': 'S', 'было': 'V', 'весьма': 'ADV', 'популярным': 'A', 'законодательное': 'A', 'закрепление': 'S', 'этот': 'APRO', 'термин': 'S' ...}

Required: Find all 'А'dictionary values ​​and pull out their key, but only if the next word has a value of 'S' .
According to the given dictionary (I brought a piece there), it is necessary that the words are displayed . How to do it? It seems the algorithm is simple, but I don’t understand something in any way. Thank you. 'законодательное' 'закрепление'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-10-25
@Timebird

from itertools import tee

def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

keys = [k for k, v in zip(pairwise(d.keys()), pairwise(d.values())) if v == ('A', 'S')]

S
sash999, 2018-10-25
@sash999

You can convert it to a list of tuples using dict.items(), but then everything is simple.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question