Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract specific elements from a dictionary on the basis of keys and values

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’, ‘.’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`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’]

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading