Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArcGIS中的Python-在字典中查找值,并将相应的值打印到新字段中 我对Python非常新,并且已经尝试了几天来写一个函数,它在属性表中解码一个字段,并将相应的值(从下面的字典)添加到空白字段中。(类似于excel中的vlookup)。例如,当“e1”出现在我的属性表中时,在字典中查找“e”,在字典中找到相应的值并将其打印到列表中,然后查找“1”并将其打印到同一列表中(空白字段已添加到属性表中)。然后将此列表添加到属性表的空白字段中。_Python_Dictionary_Arcgis_Vlookup - Fatal编程技术网

ArcGIS中的Python-在字典中查找值,并将相应的值打印到新字段中 我对Python非常新,并且已经尝试了几天来写一个函数,它在属性表中解码一个字段,并将相应的值(从下面的字典)添加到空白字段中。(类似于excel中的vlookup)。例如,当“e1”出现在我的属性表中时,在字典中查找“e”,在字典中找到相应的值并将其打印到列表中,然后查找“1”并将其打印到同一列表中(空白字段已添加到属性表中)。然后将此列表添加到属性表的空白字段中。

ArcGIS中的Python-在字典中查找值,并将相应的值打印到新字段中 我对Python非常新,并且已经尝试了几天来写一个函数,它在属性表中解码一个字段,并将相应的值(从下面的字典)添加到空白字段中。(类似于excel中的vlookup)。例如,当“e1”出现在我的属性表中时,在字典中查找“e”,在字典中找到相应的值并将其打印到列表中,然后查找“1”并将其打印到同一列表中(空白字段已添加到属性表中)。然后将此列表添加到属性表的空白字段中。,python,dictionary,arcgis,vlookup,Python,Dictionary,Arcgis,Vlookup,此时,您的数据结构不是您的朋友 所有这些信息都堆积在嵌套字典结构中,但最好先对其进行解码: #!python3 import collections import itertools LUC_dict = { 'estu':'Estuaries', 'ice':'Ice', 'lake':'Lake', 'quar':'Quarries/Mines', 'rive':'River', 'town':'Town/Urban', 'Class':{'1'

此时,您的数据结构不是您的朋友

所有这些信息都堆积在嵌套字典结构中,但最好先对其进行解码:

#!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 for 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']

for 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"

for test in tests.split():
    f1, f2 = decode(test)
    print("{}: [{}, {}]".format(test, f1, f2))
此操作的输出如下所示:

6e: [Non Arable (6), Erosion]
4s: [Arable (4), Soil]
rive: [River, ]
2s: [Arable (2), Soil]
estu: [Estuaries, ]

我认为这就是您的想法。

此时,您的数据结构不是您的朋友

所有这些信息都堆积在嵌套字典结构中,但最好先对其进行解码:

#!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 for 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']

for 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"

for test in tests.split():
    f1, f2 = decode(test)
    print("{}: [{}, {}]".format(test, f1, f2))
此操作的输出如下所示:

6e: [Non Arable (6), Erosion]
4s: [Arable (4), Soil]
rive: [River, ]
2s: [Arable (2), Soil]
estu: [Estuaries, ]

我想这正是你的想法。

这会奏效吗?为了清晰起见,尝试直接在您的psuedocode之后为我的解决方案建模

def decode_LUC(input_string, LUC_dict): # Changed signature to also accept the dict (keep parameter local to function)
    if not input_string.isalnum(): # Check if alphanumeric
        raise ValueError("Input string is not alphanumeric!")
    elif len(input_string) == 2: # Split if we have len 2
        part1, part2 = input_string[0], input_string[1] # Split into two strings
        return [LUC_dict["Class"][part1], LUC_dict["Subclass"][part2]]
    else: # Else try to directly access dict
        return LUC_dict[input_string]

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'}}

# If you're in Python3, wrap the print statements with parens
print decode_LUC("6e", LUC_dict) # Prints ['Non Arable (6)', 'Erosion']
print decode_LUC("estu", LUC_dict) # Prints Estuaries
print decode_LUC("e^", LUC_dict) # Raises a value error

这会奏效吗?为了清晰起见,尝试直接在您的psuedocode之后为我的解决方案建模

def decode_LUC(input_string, LUC_dict): # Changed signature to also accept the dict (keep parameter local to function)
    if not input_string.isalnum(): # Check if alphanumeric
        raise ValueError("Input string is not alphanumeric!")
    elif len(input_string) == 2: # Split if we have len 2
        part1, part2 = input_string[0], input_string[1] # Split into two strings
        return [LUC_dict["Class"][part1], LUC_dict["Subclass"][part2]]
    else: # Else try to directly access dict
        return LUC_dict[input_string]

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'}}

# If you're in Python3, wrap the print statements with parens
print decode_LUC("6e", LUC_dict) # Prints ['Non Arable (6)', 'Erosion']
print decode_LUC("estu", LUC_dict) # Prints Estuaries
print decode_LUC("e^", LUC_dict) # Raises a value error