Select to view content in your preferred language

Using Python Dictionaries to Change Feature Class Names

3042
11
Jump to solution
04-30-2020 08:48 AM
NataliaGutierrez1
Regular Contributor

Hello!

I recently posted a question about python dictionaries and here I am again struggling with them..

I am now trying to use them to change feature class names.

Problem:

I have a list of 77 feature classes and I need to change their names from codes to names using a python dictionary.

For example:

Feature class name: T14_single_points

New name that I need: Alachua

I am not getting any errors when running this code but I am not getting any results either.

import arcpy

# script to change fc name using a dictionary

arcpy.env.workspace = r"D:\APRX_MXDS\USA_Parcels_2019_Project\test.gdb"
arcpy.env.overwriteOutput = True

fc_list = arcpy.ListFeatureClasses("points*") # list of fc that end with points

# dictionary
county_codes = {14 : "Bradford", 15 : "Brevard", 16 : "Broward"}

for fc in fc_list: # loop through the feature classes
    key = "{[1:3]}".format(fc.name) # let the second and third characters in the fc name be the key
    if key in county_codes: 
        fc = county_codes.get(key) # assign the value in the dictionary as the new feature class name‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

thank you!

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

You are looking for the values, not the keys, which could be the other part of the problem.

Examine the example.  Dictionaries are designed to be key-driven... your driver is 2 letters from your county names... aka, the values.  There is a workaround, if you must stick to dictionaries.

codes = {14 : "Bradford", 15 : "Brevard", 16 : "Broward"}
kys = codes.keys()
vals = codes.values()
v = [v[1:3] for v in vals]

kys  # ---- dict_keys([14, 15, 16])
vals # ---- dict_values(['Bradford', 'Brevard', 'Broward'])
v    # ---- ['ra', 're', 'ro']

look_for = 'ra'  # ---- Let's look for Bradford, ie, 'ra'

for k, v in codes.items():
    if look_for in v:
        print("found look_for")
    else:
        print("keep looking")
        
found look_for
keep looking
keep looking

Of course you can skip the dictionary thing and just use nested lists

l = [[14, 'Bradford'], [15, 'Brevard'], [16, 'Broward']]

look_for = 'ra'
for k, v in l:
    if look_for in v:
        print("found {}".format(v))
    else:
        print("keep looking")
        
found Bradford
keep looking
keep looking

And numpy structured arrays are my preference because slicing is enhanced and the operation can be vectorized on whole inputs columns rather than by row... but I will leave that since I am not sure where you are going with this.

NataliaGutierrez1
Regular Contributor

Thank you Dan. In the end I had to do some more research on the topic before I was able to automate this. 

Because I needed this very urgent I decided to do it manually since it was taking me to long to automate. 

I am using your answer as a guide for the next time I have to do something similar.

0 Kudos