0

I have a field, "Access" that I want to populate with 4 variables based on the attribute of another field, "Des_Tp".For example, if a feature has "Des_Tp" = 'NP', then I would want to code it as open access, 'OA' in the field "Access".

I've put the lists in a dictionary, but the code runs without any change in the data:

import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
destp_dict ={1:("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"),
2:("NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"),
3:("MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"),
4:("WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE")}

fields = ['Des_Tp', 'Access']

arcpy.MakeFeatureLayer(inFeatures, "TEMP_LYR")

with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
    for row in cursor:
        if row[0] == destp_dict[1]:
            row[1] = "OA"
        if row[0] == destp_dict[2]:
            row[1] = "RA"
        if row[0] == destp_dict[3]:
            row[1] = "UK"
        if row[0] == destp_dict[4]:
            row[1] = "XA"
        cursor.updateRow(row)
5
  • 2
    You'll likely want to refactor that section since you are opening 63 cursors. Probably simpler to populate a dictionary with your 63 variables (I assume this is coming from the table?), and perform a lookup inside 1 update cursor.
    – Paul
    Commented May 5, 2017 at 21:25
  • 2
    If you don't want to refactor, it's likely failing because UK_list has only 8 entries, so it's erroring out on the 9th iteration.
    – Paul
    Commented May 5, 2017 at 21:27
  • @Paul, great idea. I was trying to play with dictionaries, but have no experience with them. I'll update my version with the dictionary in the post above, but would need help getting it to work. Thanks!
    – saoirse
    Commented May 5, 2017 at 21:48
  • 1
    With the edit to your question it is now difficult to tell where you are currently stuck because your two code snippets mean that we are effectively trying to answer two questions. Would you be able to revise your question so that it focuses on only one, please? Also, you are using an object dm that is not defined in either code snippet.
    – PolyGeo
    Commented May 5, 2017 at 22:41
  • @PolyGeo, thanks for the clarification. I've simplified my question and updated above
    – saoirse
    Commented May 9, 2017 at 21:19

1 Answer 1

1

You need to check if a value is in your list, not if it == your list.

The value in row[0] doesn't equal ("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"), it equals NP, so you are wanting to know if the value is in that list.

You can do this by using if 'NP' in ("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"):, or in your cursor, to look up in your dictionary you'd use if row[0] in destp_dict[1]:

import arcpy
arcpy.env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
fields = ['Des_Tp', 'Access']
destp_dict = {1: ("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"),
              2: ("NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"),
              3: ("MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"),
              4: ("WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE")}

with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
    for row in cursor:
        if row[0] in destp_dict[1]:
            row[1] = "OA"
        elif row[0] in destp_dict[2]:
            row[1] = "RA"
        elif row[0] in destp_dict[3]:
            row[1] = "UK"
        elif row[0] in destp_dict[4]:
            row[1] = "XA"
        cursor.updateRow(row)

So that should get your code working for you, however I don't think there's any real benefit in using dictionaries in this way as it makes it harder to store and view the values. Instead I'd just store four lists and lookup each list:

import arcpy
arcpy.env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
fields = ['Des_Tp', 'Access']
oa = ["NP", "NM", "NCA", "NF", "NG", "PUB", "NT", "WSR", "NRA", "NLS", "IRA", "ACEC", "REC", "RMA", "ACC", "OCS", "SP", "SREC", "SLHCA", "SRMA", "LP", "LREC", "LHCA", "LRMA"]
ra = ["NWR", "WA", "WSA", "NSBV", "RNA", "REA", "HCA", "SDA", "SW", "SCA", "LCA"]
uk = ["MPA", "PROC", "FOTH", "ND", "TRIBL", "SO", "LO", "UNK"]
xa = ["WPA", "MIT", "MIL", "PCON", "PREC", "PHCA", "PAGR", "PRAN", "PFOR", "PO", "PO", "CONE", "RECE", "HCAE", "HCAE", "AGRE", "RANE", "FORE", "OTHE", "UNKE"]

with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
    for row in cursor:
        if row[0] in oa:
            row[1] = "OA"
        elif row[0] in ra:
            row[1] = "RA"
        elif row[0] in uk:
            row[1] = "UK"
        elif row[0] in xa:
            row[1] = "XA"
        cursor.updateRow(row)
1
  • Thank you Midavalo, that did the trick. I also agree with the separation of lists, seems unnecessary to create a key that wouldn't make any sense without reference.
    – saoirse
    Commented May 10, 2017 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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