I have the following dict:
{('I', 'like'):14, ('he','likes'):2, ('I', 'hate'):12}
For a given word
string I want to get the second element of all tuples in dictionary (which is a key of a dictionary) that has this word
as the first element.
I tried:
word='I'
second_word = (k[0][1] for k, v in d if word == k[0][0])
print(second_word)
and expected to get "like" as an answer but got:
<generator object generate_ngram_sentences.<locals>.<genexpr> at 0x7fed65bd0678>
<generator object generate_ngram_sentences.<locals>.<genexpr> at 0x7fed65bd0678>
<generator object generate_ngram_sentences.<locals>.<genexpr> at 0x7fed65bd06d0>
- How to get not only first occurrence but all of such occurrences in dictionary?
EDIT: 2. Can you share how could it be modified in case the size of the tuple to be dynamic. So that the key of the dict would store eg. 2elem tuple or 15elem etc. tuple depending on dict?