SearchCursor, create unique lists, convert to dictionary

5960
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
MatthewRusso
New Contributor II

Don't know what happened but i can't find the post editor.

Basically I am getting an error:

Runtime error

Traceback (most recent call last):

  File "<string>", line 12, in <module>

  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 1044, in getValue

    return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))

RuntimeError: ERROR 999999: Error executing function.

I am trying to create 2 list via the search cursor, then put them in a dictionary.  I might be doing this completely wrong but what I am doing makes sense to me.  I can run through the first list no problem, but get an error when i add the second list.

Any help would be awesome.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Post manage options (edit and delete) are under the Actions menu at the extreme bottom left of each post.

0 Kudos
MatthewRusso
New Contributor II

It's showing up for my reply but not the main post

0 Kudos
BlakeTerhune
MVP Regular Contributor

Ah, yes, forgot about that. The actions for the original post are at the top of the post off to the right (under the following, share, bookmark, and like options).

0 Kudos
XanderBakker
Esri Esteemed Contributor

The error probably refers to the problem obtaining a value using getValue. You may check the name of the field (does it exist) and the content of the field (empty values?).

Are you using 10.0? If you have access to 10.1 (SP1) you can use the data access module that makes life a lot easier:

To create a dictionary from OID and an arbitrary field:

fc = r'C:\path\to\your\featureclass\or\table'

fld_oid = arcpy.Describe(fc).OIDfieldname

fld_other = 'afieldname'

d = {r[0]: r[1] for r in arcpy.da.SearchCursor(fc, (fld_oid, fld_other))}

What should the dictionary contain?

Some more info and useful snippets can be found here: Some Python Snippets

MatthewRusso
New Contributor II

Dictionary should contain the County ID/County Name to be used later to run through an extraction process.

I am on 10.1 SP1 Adv Copy

fc = "Texas" is a fc in a geodatabase

I am pulling parcel data out of 10 states so i want to pull the info out of the tables for my selection process and naming convention.  I have to do this on a weekly basis coming up so I am trying to have a script to run overnight.

The parcel file is joined to another file, could this effect it?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Try this and see if it works:

import arcpy

# set your workspace

# (or not if you run this in the Python Window and the layer Texas is in your TOC)

# arcpyy.env.workspace = r"drive:\path\to\your\input\fgdb.gdb" # or folder

fc = 'Texas'

field1 = "COUNTY_CODE"

field2 = "NAME10"

# create dictionary

d = {r[0]: r[1] for r in arcpy.da.SearchCursor(fc, (field1, field2))}

for key,val in d.items():

    print key,val

0 Kudos
BlakeTerhune
MVP Regular Contributor

Does using a search cursor in a list (dictionary) comprehension like this automatically close the cursor when it's finished like when using it in a with block?

EDIT:

I did a quick test and it looks like the lock does go away. Spiffy!

0 Kudos
MatthewRusso
New Contributor II

So i'm testing the da.searchcursor right now, i believe there is some issue with joining the 2 Feature class tables.

my first test i ran with 1 of my fields the "COUNTY_CODE" and it worked, created a unique list fine

I then joined my files and ran the exact same script and it said "Cannot find field 'COUNTY_CODE'"

I am trying to export to a new file to see if the join was acting like a temp file or something, kind of strange.

Here is where I am at.  I don't want to make it a dictionary right of the bat so i can test it vs. all the states.

fc = 'Texas2'

field1 = "COUNTY_CODE"

field2 = "NAME10"

list1 = [row[0] for row in arcpy.da.SearchCursor(fc, field1)]

list2 = [row[0] for row in arcpy.da.SearchCursor(fc, field2)]

#Get Unique Values

uValue1 = set(list1)

uValue2 = set(list2)

#Convert to a List

uList1 = list(uValue1)

uList2 = list(uValue2)

print(uValue1)

print(uValue2)

#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

0 Kudos