SearchCursor, create unique lists, convert to dictionary

5952
13
02-05-2015 09:12 AM
MatthewRusso
New Contributor II

fc = 'Texas'

field1 = "COUNTY_CODE"

field2 = "NAME10"

list1 = []  

list2 = []

rows = arcpy.SearchCursor(fc)

for row in rows:

    list1.append(row.getValue(field1))

    list2.append(row.getValue(field2))

else:

    print "loop failed"

#Get Unique Values from the List        

uSet1 = set(list1)

uSet2 = set(list2)

#Convert the set to a list

uList1 = list(uSet1)

uList2 = list(uSet2)

#Print The list

print uList1

print uList2

#Create Dictionary from the 2 lists and print results

from itertools import chain, repeat

d = dict(zip(uList1, chain(uList2, repeat(None))))

for key,val in d.items():

    print key,val

Tags (2)
0 Kudos
13 Replies
BlakeTerhune
MVP Regular Contributor

Maybe you have to call your field name as "featureclass.fieldname" beacuse that's how ArcMap renames when you join. Try using ListFields to print the name of the fields on the feature class you're running the SearchCursor on (Texas2) so you can see what you're working with.

fields = arcpy.ListFields(fc)

for f in fields:

    print f.name

EDIT:

Are you doing the join in Python?

ArcGIS Help 10.1 - Add Join (Data Management)

0 Kudos
MatthewRusso
New Contributor II

EDIT: I have joined it via GP Tool

Ok, progress has been made.  I ran what you listed above and it looks like my fields are showing up as seperate files still(guessing because of the Join)

I tried to make the join permenate by exporting/copying feature but it isn't working since the fields are still linking to the joined file.

Notice the print of f.name

#Texas FC

Texas_COUNTY_CODE

Texas_STATE_CODE

#Texas_Counties FC

Texas_Counties_FID

Texas_Counties_STATEFP10

Texas_Counties_COUNTYFP10

Texas_Counties_NAME10

It seems like it is working, just can't find that field because it is not displaying as the "Texas" FC

0 Kudos
BlakeTerhune
MVP Regular Contributor

Try doing the join in Python using arcpy.AddJoin_management() (see example 2 at the bottom). Once you've done the join, then print the field names and see what you need to use for the search cursor.

0 Kudos
BlakeTerhune
MVP Regular Contributor

After further investigation, I should clarify that the feature class needs to be a feature layer before you can do the join. Once you do the join, the field names on the feature layer are indeed "featureclassname.fieldname".

0 Kudos