Select to view content in your preferred language

How to populate a list using multiple fields from a table?

1912
4
02-23-2013 08:14 AM
ChristiNelson1
Deactivated User
Hi all,

I'm using ArcGIS 10.0 (ArcInfo level). Below is a script that creates an empty list, loops thru a parcels feature class, gets the value for each record in the 'APN_NU_1' field and populates the list with the values.  This runs perfectly.  How do I write this script to populate the list with multiple fields and their values, say 'APN_NU_1', 'ADDR_B', and 'CITY'?  I will then be using this list to call for further programming..

import modules and define workspace
import arcpy
import os
import traceback


workspace = arcpy.env.workspace = "E:/dev/Christi/PSL/" 

arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument("CURRENT")


#List the first dataframe (named Layers) in the map document
df = arcpy.mapping.ListDataFrames(mxDoc, "Layers") [0]


#List first map layer (which is the parcels layer) in dataframe
parcelLayer = arcpy.mapping.ListLayers(mxDoc,"",df) [0]


#Select parcel layer by attribute and clear any previously selected features
#Set up a Search Cursor to go thru the attribute table and get row values 
#Set up variable, then read the row values for 'APN_NU_1' field and populate the list

arcpy.SelectLayerByAttribute_management(parcelLayer,"CLEAR_SELECTION")

rows = arcpy.SearchCursor(parcelLayer)
stringList = []
for row in rows:
        stringList.append(row.getValue('APN_NU_1'))


Thanks for any immediate feedback,
Christi
Tags (2)
0 Kudos
4 Replies
by Anonymous User
Not applicable
Hi Christi,

Are you familiar with dictionaries?  This would be a great way to generate a list for each variable that could be called by using the parcel ID.  I included an example of how to create a dictionary with all 3 attributes you wanted.  These attributes can be accessed by the unique parcel ID which would serve as the key for the dictionary.  The example I showed at the bottom demonstrates how to print
"Parcel ID:    address, city"

For example, it would show something like this:

0360-15-16-200-002-0:    101 Main St, Springfield

The advantage of using a dictionary like this is that you can keep all these items grouped together by parcel ID.  If you wanted to add these fields to another table, you could do so easily if the second table has the common Parcel ID field without even needing to join the tables.  You could simply use an update cursor with this dictionary.

#import modules and define workspace
import arcpy
import os
import traceback


workspace = arcpy.env.workspace = "E:/dev/Christi/PSL/" 

arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument("CURRENT")


#List the first dataframe (named Layers) in the map document
df = arcpy.mapping.ListDataFrames(mxDoc, "Layers") [0]


#List first map layer (which is the parcels layer) in dataframe
parcelLayer = arcpy.mapping.ListLayers(mxDoc,"",df) [0]


#Select parcel layer by attribute and clear any previously selected features
#Set up a Search Cursor to go thru the attribute table and get row values 
#Set up variable, then read the row values for 'APN_NU_1' field and populate the list

arcpy.SelectLayerByAttribute_management(parcelLayer,"CLEAR_SELECTION")

info_dict = {}
rows = arcpy.SearchCursor(parcelLayer)
for row in rows:
    info_dict[row.Parcel_ID] = [row.APN_NU_1,row.ADDR_B,row.CITY]
    
del row,rows

# If you want to access a single field you can reference it with a list index
# example:  if you want the address B for a certain parcel you can print something
# like this:  print info_dict[row.Parcel_ID][1]
# the [1] indexes item 1 (value) in your list for each dictionary key (parcel ID)
# So to print each address, city for each Parcel ID:

for key,value in info_dict_iteritems():
    print '%s:\t%s, %s' %(key,value[1],value[2])
        
0 Kudos
ChristiNelson1
Deactivated User
Thanks so much Caleb - this is exactly the direction I thought I needed to go in, but just didn't know how.  Question for you:  what is info_dict_iteritems() equal to in this case?
0 Kudos
MathewCoyle
Honored Contributor
He meant
info_dict.iteritems()
0 Kudos
by Anonymous User
Not applicable
He meant
info_dict.iteritems()


Yes, that was supposed to be a period not underscore, thanks Mathew!  I just used the iteritems() method to demonstrate how to iterate through a dictionary and in that case to pull keys and values separately and print them.  You mentioned you wanted to use these values later in your script, what are you going to do with them?  Using a dictionary will allow you to have the list of those 3 attributes tied to each Parcel ID (or whatever unique identifier you want to use as the key for the dict) and you can access a specific one by indexing the list.
0 Kudos