Python in ArcGIS - Lookup values in a dictionary and print corresponding value into a new field -
i new python , have been trying days work write function decodes field in attribute table , adds corresponding value (from dictionary below) blank field. (similar vlookup in excel). example when 'e1' shows in attribute table 'e' in dictionary, find corresponding value in dictionary , print list , '1' , print same list (blank field has been added attribute table). add list blank field in attribute table.
your data structure not friend @ point.
you have information stacked in nested dictionary structure, better off decoding first:
#!python3 import collections import itertools luc_dict = { 'estu':'estuaries', 'ice':'ice', 'lake':'lake', 'quar':'quarries/mines', 'rive':'river', 'town':'town/urban', 'class':{'1':'arable (1)', '2':'arable (2)', '3':'arable (3)', '4':'arable (4)', '5':'non arable (5)', '6':'non arable (6)', '7':'non arable (7)', '8':'protected (8)'}, 'subclass':{'c':'climiate', 'e': 'erosion', 's': 'soil', 'w': 'wetness'}} field1_value = {k:v k,v in luc_dict.items() if k not in ('class','subclass')} field2_value = collections.defaultdict(str) classes = luc_dict['class'] subclasses = luc_dict['subclass'] c,sc in itertools.product(classes.keys(), subclasses.keys()): field1_value[c+sc] = classes[c] field2_value[c+sc] = subclasses[sc] def decode(instr): return field1_value[instr], field2_value[instr] tests = "6e 4s rive 2s estu" test in tests.split(): f1, f2 = decode(test) print("{}: [{}, {}]".format(test, f1, f2))
the output looks like:
6e: [non arable (6), erosion] 4s: [arable (4), soil] rive: [river, ] 2s: [arable (2), soil] estu: [estuaries, ]
which think have in mind.
Comments
Post a Comment