Home > Software engineering >  Extract specific elements from a dictionary on the basis of keys and values
Extract specific elements from a dictionary on the basis of keys and values

Time:01-19

pos_dict = {'I': 'PRON', 'feel': 'VERB', 'that': 'SCONJ', 'there': 'PRON', 'should': 'AUX', 'not': 'PART', 'be': 'AUX', 'fight': 'NOUN', 'in-between': 'ADP', 'marriage': 'NOUN', '.': 'PUNCT'}

contiguous_values = []
temp = []

for key, value in pos_dict.items():
    if value == 'PRON':
        if temp:
            contiguous_values.append(temp)
        temp = [key]
    elif value == 'NOUN':
        temp.append(key)
        contiguous_values.append(temp)
        temp = []
    else:
        if temp:
            temp.append(key)

print(contiguous_values)

From this pos_dict, I want to extract the words in the sequence :-

  • The word extracted first should start from 'PRON' and ends with 'NOUN'
  • But, If after the first discovered 'NOUN' there are few more nouns in the sentence then
  • Extract till those nouns ,until the next 'PRON' is found

The pattern should be this - 'there': 'PRON', 'should': 'AUX', 'not': 'PART', 'be': 'AUX', 'fight': 'NOUN', 'in-between': 'ADP', 'marriage': 'NOUN', '.': 'PUNCT'

I want to extract this - 'there', 'should', 'not', 'be', 'fight', 'in-between', 'marriage', '.'

`pos_dict = {'I': 'PRON', 'feel': 'VERB', 'that': 'SCONJ', 'there': 'PRON', 'should': 'AUX', 'not': 'PART', 'be': 'AUX', 'fight': 'NOUN', 'in-between': 'ADP', 'marriage': 'NOUN', '.': 'PUNCT'}

contiguous_values = [] temp = []

for key, value in pos_dict.items(): if value == 'PRON': if temp: contiguous_values.append(temp) temp = [key] elif value == 'NOUN': temp.append(key) contiguous_values.append(temp) temp = [] else: if temp: temp.append(key)

print(contiguous_values)`

I found this - [['I', 'feel', 'that'], ['there', 'should', 'not', 'be', 'fight'], ['marriage']]

But, I want - ['there', 'should', 'not', 'be', 'fight', 'in-between', 'marriage']

CodePudding user response:

Try this -

contiguous_values = []
temp = []

for key, value in pos_dict.items():
    if value != 'PRON':
        temp.append(key)
    else:
        if temp:
            contiguous_values.append(temp)
        temp = []

# check if the last element is not 'PRON'
if temp:
    contiguous_values.append(temp)

print(contiguous_values)
  • Related