0

I have a layer I have created and assigned unique IDs. I only have a standard Arc license and require only one of each of the rows with a unique ID. Is there any way to select these using python? Or a work around where I can label them and select from there? These items do not always have duplicate geometry.

An example of what I hope to achieve is below:

enter image description here

(I have added the ORDER_ column and populated manually purely as an example)

Alan Carr
  • 2,475
  • 6
  • 28
  • 44

3 Answers3

1

In python you could create an array and make a statement telling it to go through your list using arcpy.SearchCursor and check if the ID is already in the array. If it's not there, add it to the list, if it is, then do nothing and go onto the next record. Once it's completed this "check" you can tell it to select by attributes based on the values in your array. Be sure to use "Add to current selection".

Oly
  • 51
  • 5
  • Alternatively, he could create an iterator to go through and automate the creation of his "ORDER_" field. And then select all rows with a value of "1" in that field. – Jvhowube Oct 10 '16 at 15:06
1

Summary Statistics is available at any license level. Enter FID, Min for the Statistics Field. For the Case Field, enter SECTIONLAB. The resulting table should contain the first record for each unique SECTIONLAB. You can join this table back to the original table.

klewis
  • 7,475
  • 17
  • 19
0

collections.defaultdict is perfect for this. If you use a counter, choosing the last would be trickier (not required in this instance, but potentially useful nonetheless).

from collections import defaultdict
import arcpy

ddict = defaultdict(list)

for key,oid in arcpy.da.SearchCursor("my fc", ["keyField", "OID@"]):
    ddict[key].append(oid)

# Get the first distinct value. Use -1 to access the last
oids = sorted([v[0] for v in ddict.itervalues()])

oidField = arcpy.AddFieldDelimiters("my workspace", arcpy.Describe("my fc").oidFieldName)
sql = "{} IN ({})".format(oidField, ",".join(map(str, oids)))

With the following table, gridcode is my keyfield:

| OBJECTID | gridcode |
|----------|----------|
| 1        | 6        |
| 2        | 6        |
| 3        | 5        |
| 4        | 5        |
| 5        | 6        |
| 6        | 6        |
| 7        | 5        |
| 8        | 1        |
| 9        | 5        |

>>> print(sql)
OBJECTID IN (1,3,8)

And for the last distinct value:

oids = sorted([v[-1] for v in ddict.itervalues()])
>>> print(sql)
OBJECTID IN (6,8,9)
Paul
  • 11,608
  • 1
  • 29
  • 47