-1

I need to extract specific words from the list of attributes and return all the possible words to a new field with the help of defined dictionaries. (using Python Parser in ArcGIS)

Example

  1. Apples are red in colour
  2. Grapes are green in colour
  3. Grepes are sour in taste
  4. Mangoes are sweet to taste
  5. Mnagoes are yellow in colour

Defined dictionaries:

Mangoes:('Mangoes','Mnagoes')
Grapes: ('Grapes','Grepes')

Expected Output:

  1. Apples,red
  2. Grapes,green
  3. Grapes,sour
  4. Mangoes,sweet
  5. Mangoes,yellow

enter image description here

This is the code i tried with:

def replace(work_name): 
    if any(x in work_name.lower() for x in ['dam','dem','daem']): 
        return 'Dam' 
    elif any(x in work_name.lower() for x in ['pond','pound','talab']): 
        return 'Farm Pond' 
    elif any(x in work_name.lower() for x in ['plantation']): return 'Plantation' 
    else: return 0
  • def replace(work_name): if any(x in work_name.lower() for x in ['dam','dem','daem']): return 'Dam' elif any(x in work_name.lower() for x in ['pond','pound','talab']): return 'Farm Pond' elif any(x in work_name.lower() for x in ['plantation']): return 'Plantation' else: return 0 – neeraja kolipe Mar 12 '20 at 8:42
  • Please check the added image – neeraja kolipe Mar 12 '20 at 8:48
  • Field Calculator – neeraja kolipe Mar 12 '20 at 9:07
0

I am not quite sure I understand the fruit example. But looking at your code, the output, and the expected output, I think your problem is that you are returning as soon as you find a keyword. In that way you are always going to get just one keyword. Try something like this,

def replace(work_name):
    keys = []
    if any(x in work_name.lower() for x in ['dam','dem','daem']): 
        keys.append('Dam') 
    if any(x in work_name.lower() for x in ['pond','pound','talab']): 
        keys.append('Farm Pond')
    elif any(x in work_name.lower() for x in ['plantation']):
        keys.append('Plantation')
    if len(keys) == 0:
        return 0
    return ','.join(keys)

One thing to note is, the order of the keys do not correspond to the appering order of the words that match the key. I am not sure if you need this as well.

  • Thank you so much. This is working. – neeraja kolipe Mar 16 '20 at 5:45
  • Great, glad it helps!. Please mark the answer as correct. – cabesuon Mar 16 '20 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.