0

I have dictonary named as Entity which look like this:

Entity = {'ORG': 'ABC','PERSON': 'Ankit','ORDINAL': '150th','DATE': 'quarterly', 'MONEY': 'dollars'}

Now I have to create other dictionary which will contain below value according to condition.

Class = {'Signal':'<Condition>'}

The condition is:

  • If Entity key consists of ORG and PERSON then in Class dictionary value "Medium" should be updated.

  • If Entity key consists of ORG, PERSON, and MONEY then in Class dictionary value "Strong" should be updated.

  • If Entity key doesn't consist of above keys then in Class dictionary value "Weak" should be updated.

How should I make the condition for the above problem?

I want an output in Class dictionary like below,

If in Entity dictionary there are ORG and PERSIN keys then Class dictionary should be like,

Class = {'Signal' : 'Medium'}
Amir Shabani
  • 3,857
  • 6
  • 30
  • 67
Ankit Rathi
  • 109
  • 1
  • 9
  • 2
    Possible duplicate of [Python check if list of keys exist in dictionary](https://stackoverflow.com/questions/10995172/python-check-if-list-of-keys-exist-in-dictionary) – Guillaume May 28 '19 at 13:19

3 Answers3

0

To check if dictionary contains specific key use: 'ORG' in Entity statement which returns True if given key is present in the dictionary and False otherwise.

maslak
  • 1,115
  • 3
  • 8
  • 22
0

You can use the .keys() function on the dictionary to check before accessing the data structure. For example:

def does_key_exist(elem, dict):
    return elem in dict.keys()
Colby
  • 313
  • 4
  • 13
0
if all([a in Entity for a in ('ORG', 'PERSON', 'MONEY')]):
    Class['Signal'] = 'strong'
elif all([a in Entity for a in ('ORG', 'PERSON')]):
    Class['Signal'] = 'middle'
elif not any([a in Entity for a in ('ORG', 'PERSON', 'MONEY')]):
    Class['Signal'] = 'weak'

Note that, according to your question, if Entity contains only 'ORG' or only 'PERSON', no 'Signal' will be set in Class.

vurmux
  • 9,420
  • 3
  • 25
  • 45