-1

From the below table: If number of ID_T's counts is more than 2 then, I have to select, same ID_T but DIST value vary of minimum 10 meters

enter image description here

closed as off-topic by PolyGeo Apr 26 at 7:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You have to add your best code attempt or your question will be put on hold. Which 11138 rows in your example table would be selected? All 11138 since 82.88-8.13 > 10 (max-min DIST)? – BERA Apr 26 at 6:35
  • Dear please check it once BERA – rakesh Apr 26 at 6:54
1

You can use collections.defaultdict to Group dist values together by id:

import arcpy
from collections import defaultdict

layer = 'somedata' #Change
fields = ['ID_T','DIST'] #Change

#Create a dictionary of id_t as key, and a list of all dist as value
d = defaultdict(list)
with arcpy.da.SearchCursor(layer,fields) as cursor:
    for idt, dist in cursor:
        d[idt].append(dist)

#If more than two values in list, and max-min>10 append id_t to a new list
idtlist = []
for idt, dist in d.iteritems(): #d.items() in py3/ArcGIS Pro
    if len(dist)>2 and max(dist)-min(dist)>10:
        idtlist.append(idt)

#A tuple of only one value will cause sql syntax error in selection below
if len(idtlist)==1:
    idtlist.append(-9999)

#Select by idt list
sql = "{0} IN{1}".format(arcpy.AddFieldDelimiters(layer, fields[0]), tuple(idtlist))
arcpy.SelectLayerByAttribute_management(in_layer_or_view=layer, 
                                       where_clause=sql)

enter image description here

  • it showing error in 9th line – rakesh Apr 26 at 8:37
  • @rakesh What is the error message? Is ID_T a text (string) field or number field (Double, INT)? – BERA Apr 26 at 8:37
  • it is a string error – rakesh Apr 26 at 9:38
  • Ok remove the two lines after comment #A tuple of only one value and change sql line to: sql = "{0} IN({1})".format(arcpy.AddFieldDelimiters(layer, fields[0]), ', '.join("'{0}'".format(i) for i in idtlist)) – BERA Apr 26 at 10:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.