How to identify unique values in selected set in geoprocessor (Python)

639
8
08-18-2010 11:09 AM
AlisaWade
New Contributor
Greetings -
In my script, I select out points based on the value in one field. I want to then calculate the number of unique values in another field within that selected set.

So, I select the set:
gp.SelectLayerByAttribute("TempData", "NEW_SELECTION", sBlockField + " >= 90")

Then I can easily access the number of selected records:
gp.GetCount("TempData").GetOutput(0)

But how do I get the unique values in "Field2" of the selected set?

I searched these and older forums; I don't see anything along these lines.
Any thoughts appreciated!
0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus
if you have a list, then use set

>>> stuff = [1,1,1,1,1,2,2,2,3,4,4,4,4,4,5,5,5,5,1,1,1,1,1]
>>> stuff_set = set(stuff)
>>> print stuff_set
set([1, 2, 3, 4, 5])

then count the set, if you then need code for frequencies, steal defs out of
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=BDF21676-1422-2418-34E6-A29C6...
0 Kudos
AlisaWade
New Contributor
if you have a list, then use set

>>> stuff = [1,1,1,1,1,2,2,2,3,4,4,4,4,4,5,5,5,5,1,1,1,1,1]
>>> stuff_set = set(stuff)
>>> print stuff_set
set([1, 2, 3, 4, 5])

then count the set, if you then need code for frequencies, steal defs out of
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=BDF21676-1422-2418-34E6-A29C6...


Thank you Dan. That helps, and I can see many, many uses for the Descriptive Statistics (not to mention some past efforts where I poorly kludged something together that your script would have solved much more elegantly)!

I think the thing I am missing is how to create the list; how do I dump a list of the values in a field in selected set of attributes to a list? Is MakeTableView the best way to do that? This would be iterative, as I'd do it within a while loop. Then I can run your descriptive statistics tool on that. Is that the fastest way? Thanks again for any help.
0 Kudos
ChrisMathers
Occasional Contributor III
Dan's tool contains the search cursors in the classes of the tool so you dont need to make a list of the field contents to use it. If you wanted to code out the stats yourself instead of using the tool you would make a list and do yourlist.append(row.getvalue("field")) in the while loop for the cursor. If you only need the stats provided by Dan's tool though I would use that as it will save you some lines in your script and make it cleaner looking. Kudos to you by the way Dan, that tool is a great solution to not having field stats in python.
0 Kudos
BruceHarold
Esri Regular Contributor
Hi

List comprehensions are an ideal way to get a list of field values:

myList = [f.getValue(someField) for f in arcpy.SearchCursor(someFC)]

SearchCursor arguments can include a SQL query, or can access a feature layer and therefore its selection set.

Regards
0 Kudos
DanPatterson_Retired
MVP Emeritus
Bruce, interesting point, however, I will have to recheck my benchmarking since the repetitive calls to the searchcursor seemed to be worse than my approach (ie ...SearchCursor(someFC)] ), I will recheck in arcpy to see if there is a speed difference...in the interim, other may steal my code as they see fit.  If you have any benchmarking/timing information I would appreciate an update
0 Kudos
AlisaWade
New Contributor
I didn't do a timing test, but I did use the repetitive calls to an update cursor; I put another where clause in front of each call to the update cursor so that I limited where it looked based on another field that I changed. The speed was ok (certainly not fast, but not painfully slow either). Thanks to everyone for the help; very appreciated.
0 Kudos
DaleHoneycutt
Occasional Contributor III
Use the Summary Statistics tool.  Assuming that "Field2" is your field, add Field2 as your statistics field and get the COUNT statistic.  For the Case Field, input Field2 as well.  You'll get an output table that has each unique value of Field2 as well as the number of times that value appears.

I should add that you should write the output of Summary Statistics to in_memory.  See Using in_memory workspace.  You can write to in_memory in Python.
0 Kudos
curtvprice
MVP Esteemed Contributor
Bruce, interesting point, however, I will have to recheck my benchmarking since the repetitive calls to the searchcursor seemed to be worse than my approach (ie ...SearchCursor(someFC)] ), I will recheck in arcpy to see if there is a speed difference...in the interim, other may steal my code as they see fit.  If you have any benchmarking/timing information I would appreciate an update


Good news, at 10.1 we are told to expect much faster performance from cursors.
0 Kudos