<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic ArcPy: More efficient method to cycle through code/selection of records in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611770#M47756</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm having a bit of trouble finding a more suitable way to handle a series of selections/queries based on a many-to-many relationship.&lt;/P&gt;&lt;P&gt;The code below works, for the most part, but it's in the "def selectRelatedRecords" section where I know I'm probably unnecessarily repeating steps and could use some input.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The basic idea:&amp;nbsp; I have a table (linkTable) where the user selects a field and a value from that table that becomes sqlExp.&lt;/P&gt;&lt;P&gt;I then get the value of the field "CR_ID" from the same linkTable where sqlExp is true.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I then want to cycle through all the feature layers in the current mxd and get the related records in those layers (again, CR_ID is the same as the now selected records in linkTable.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But what I end up doing is reselecting the same records over and over in the linkTable each time I try to apply a SelectLayerByAtrribute to the available feature layers b/c I'm looping for lyr in layers:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm really looking for some tweaks so I can maybe put the selected CR_IDs into a list, maybe, and then apply the selectlayerbyattribtue to the available layers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os, string
import arcpy.mapping as MAP


#Needed CR layers as well as Link table should be loaded into current mxd
mxd = MAP.MapDocument("CURRENT")
df = MAP.ListDataFrames(mxd)[0]


#Get Link tables from input
linkTable = arcpy.GetParameterAsText(0)
#Make lists of all feature layers in dataframe
layers = MAP.ListLayers(mxd, "", df)




#set Reg_Code value for regional data into a sql expression
fldName = arcpy.AddFieldDelimiters(arcpy.env.workspace, arcpy.GetParameterAsText(1))
sqlExp = fldName + " = " + " '{0}' ".format(arcpy.GetParameterAsText(2))
arcpy.AddMessage(sqlExp)


#set selection type
selectType = arcpy.GetParameterAsText(3)


#used in generating list of related CR_IDs for match Link table to CR Features.
linkKeyField = "CR_ID"
featureKeyField = "CR_ID"




#Main code to select all records in CR Feature Classes with matching Program ID in Link table
def buildWhereClauseFromList(linkTable, linkKeyField, valueList):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add DBMS-specific field delimiters
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(linkTable).path, linkKeyField)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("fieldDelimited value: {}".format(fieldDelimited))


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Determine field type
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldType = arcpy.ListFields(linkTable, linkKeyField)[0].type


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add single-quotes for string field values
&amp;nbsp;&amp;nbsp;&amp;nbsp; if str(fieldType) == 'String':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; valueList = ["'%s'" % value for value in valueList]


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Format WHERE clause in the form of an IN statement
&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(map(str, valueList)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; return whereClause
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("whereClause: {}".format(whereClause))


if len(layers) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in layers:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; def selectRelatedRecords(linkTable, lyr, linkKeyField, featureKeyField):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp; Defines the record selection from the record selection of the linkTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and applys it to the featureTable using a SQL WHERE clause built
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in the previous defintion"""


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set the SearchCursor to look through the selection of the OriginTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sourceIDs = set([row[0] for row in arcpy.da.SearchCursor(linkTable, linkKeyField, sqlExp)])


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Establishes the where clause used to select records from DestinationTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = buildWhereClauseFromList(lyr, featureKeyField, sourceIDs)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("whereClause inside selectRelatedRecords: {} \n".format(whereClause))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("lyr: {0}, selectType: {1}".format(lyr, selectType))


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Process: Select Layer By Attribute
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, selectType, whereClause)


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Process: Select related records between OriginTable and DestinationTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Selecting records in CR Feature Layers related to records in Link table...")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selectRelatedRecords(linkTable, lyr, linkKeyField, featureKeyField)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 02:10:36 GMT</pubDate>
    <dc:creator>mattstutts</dc:creator>
    <dc:date>2021-12-12T02:10:36Z</dc:date>
    <item>
      <title>ArcPy: More efficient method to cycle through code/selection of records</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611770#M47756</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm having a bit of trouble finding a more suitable way to handle a series of selections/queries based on a many-to-many relationship.&lt;/P&gt;&lt;P&gt;The code below works, for the most part, but it's in the "def selectRelatedRecords" section where I know I'm probably unnecessarily repeating steps and could use some input.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The basic idea:&amp;nbsp; I have a table (linkTable) where the user selects a field and a value from that table that becomes sqlExp.&lt;/P&gt;&lt;P&gt;I then get the value of the field "CR_ID" from the same linkTable where sqlExp is true.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I then want to cycle through all the feature layers in the current mxd and get the related records in those layers (again, CR_ID is the same as the now selected records in linkTable.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But what I end up doing is reselecting the same records over and over in the linkTable each time I try to apply a SelectLayerByAtrribute to the available feature layers b/c I'm looping for lyr in layers:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm really looking for some tweaks so I can maybe put the selected CR_IDs into a list, maybe, and then apply the selectlayerbyattribtue to the available layers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os, string
import arcpy.mapping as MAP


#Needed CR layers as well as Link table should be loaded into current mxd
mxd = MAP.MapDocument("CURRENT")
df = MAP.ListDataFrames(mxd)[0]


#Get Link tables from input
linkTable = arcpy.GetParameterAsText(0)
#Make lists of all feature layers in dataframe
layers = MAP.ListLayers(mxd, "", df)




#set Reg_Code value for regional data into a sql expression
fldName = arcpy.AddFieldDelimiters(arcpy.env.workspace, arcpy.GetParameterAsText(1))
sqlExp = fldName + " = " + " '{0}' ".format(arcpy.GetParameterAsText(2))
arcpy.AddMessage(sqlExp)


#set selection type
selectType = arcpy.GetParameterAsText(3)


#used in generating list of related CR_IDs for match Link table to CR Features.
linkKeyField = "CR_ID"
featureKeyField = "CR_ID"




#Main code to select all records in CR Feature Classes with matching Program ID in Link table
def buildWhereClauseFromList(linkTable, linkKeyField, valueList):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add DBMS-specific field delimiters
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(linkTable).path, linkKeyField)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("fieldDelimited value: {}".format(fieldDelimited))


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Determine field type
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldType = arcpy.ListFields(linkTable, linkKeyField)[0].type


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add single-quotes for string field values
&amp;nbsp;&amp;nbsp;&amp;nbsp; if str(fieldType) == 'String':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; valueList = ["'%s'" % value for value in valueList]


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Format WHERE clause in the form of an IN statement
&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(map(str, valueList)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; return whereClause
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("whereClause: {}".format(whereClause))


if len(layers) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in layers:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; def selectRelatedRecords(linkTable, lyr, linkKeyField, featureKeyField):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp; Defines the record selection from the record selection of the linkTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and applys it to the featureTable using a SQL WHERE clause built
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in the previous defintion"""


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set the SearchCursor to look through the selection of the OriginTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sourceIDs = set([row[0] for row in arcpy.da.SearchCursor(linkTable, linkKeyField, sqlExp)])


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Establishes the where clause used to select records from DestinationTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = buildWhereClauseFromList(lyr, featureKeyField, sourceIDs)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("whereClause inside selectRelatedRecords: {} \n".format(whereClause))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("lyr: {0}, selectType: {1}".format(lyr, selectType))


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Process: Select Layer By Attribute
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, selectType, whereClause)


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Process: Select related records between OriginTable and DestinationTable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Selecting records in CR Feature Layers related to records in Link table...")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selectRelatedRecords(linkTable, lyr, linkKeyField, featureKeyField)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:10:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611770#M47756</guid>
      <dc:creator>mattstutts</dc:creator>
      <dc:date>2021-12-12T02:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: More efficient method to cycle through code/selection of records</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611771#M47757</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Please take a look at this sample and see if it does what you need:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.arcgis.com/home/item.html?id=e638afe0695a4ad38388cb8d9b350446" title="http://www.arcgis.com/home/item.html?id=e638afe0695a4ad38388cb8d9b350446"&gt;http://www.arcgis.com/home/item.html?id=e638afe0695a4ad38388cb8d9b350446&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Jan 2016 16:30:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611771#M47757</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2016-01-20T16:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: More efficient method to cycle through code/selection of records</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611772#M47758</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Actually, yeah.&amp;nbsp; I've seen and used that code before.&amp;nbsp; I should have started there.&amp;nbsp; This would do, provided I add a for lyr in layers: line to it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Jan 2016 16:41:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-more-efficient-method-to-cycle-through-code/m-p/611772#M47758</guid>
      <dc:creator>mattstutts</dc:creator>
      <dc:date>2016-01-20T16:41:16Z</dc:date>
    </item>
  </channel>
</rss>

