我正在创建一个函数,该函数根据输入的数字返回正确的字段名称。测试脚本只是返回第一个键,而不是期望的值(1
)。
def GetFieldName(bldgcode):
bldgcode = {1: "SFCount", 2: "MFCount", 3: "ComCount", 4: "IndCount", 5: "CityCount", 6: "ShedCount", 7: "SchCount", 8: "ChurCount"}
for values in bldgcode:
return values
#test for get field name
print(GetFieldName(8))
运行脚本给我1
时,它应该是ChurCount
直接使用dict[key]获取名称如何?
def GetFieldName(bldgcode):
code2name = {1: "SFCount", 2: "MFCount", 3: "ComCount", 4: "IndCount", 5: "CityCount", 6: "ShedCount", 7: "SchCount", 8: "ChurCount"}
return code2name[bldgcode]
#test for get field name
print(GetFieldName(8))