<?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 Re: Python double loop in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/python-double-loop/m-p/322469#M11360</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Take the filenames of the rasters and then slice out the number and then iterate over the list of masks and select the one you want by that number.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for raster in Rasters:
 number=raster[3:6]
 for mask in Masks:
&amp;nbsp; if number==mask[3:6]:
&amp;nbsp;&amp;nbsp; DO YOUR STUFF HERE
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print ('No matching mask'); break
 &lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;The reason you arent getting a good sort is the same reason sorting by a text field thats full of numbers doesnt put the numbers in the right order. Text doesnt sort like numbers and your list is all strings. This method doesnt need to be sorted to work. You just match the raster to its mask and go.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:18:50 GMT</pubDate>
    <dc:creator>ChrisMathers</dc:creator>
    <dc:date>2021-12-11T15:18:50Z</dc:date>
    <item>
      <title>Python double loop</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-double-loop/m-p/322468#M11359</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello people,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have to run the process "extract by mask" 10 times. The issue here is that I don't have the classic case where I have an input raster of which I want to extract multipe parts by looping through a folder of masks which are named with three digit numbers (eg. wsh105). I also have 10 different input files, which are also named with three digit numbers (eg. sec105). The task is to extract cells from each input raster by using the corresponding mask raster (same number in the filename). Unfortunately, if I execute the code, the two files do not correspond to each other, meaning that eg. I extract wsh105 from sec106, instead of sec 105. For testing purposes, I just list the rasters in the code below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Import system modules...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys,os, arcgisscripting&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Create the Geoprocessor object...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp = arcgisscripting.create()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Set the necessary product code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.SetProduct("ArcInfo")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Check out any necessary licenses&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.CheckOutExtension("spatial")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Allow output to overwrite...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.OverwriteOutput = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Load required toolboxes...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.AddToolbox(r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Set the Geoprocessing environment...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.snapRaster = r"C:\CPA\BaseData.gdb\fric"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.cellSize = 30&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Set the workspace...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;workspace = r"C:\CPA\Test.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;OutputSpace = r"C:\CPA\SingleCostDistances\EucDist.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Get a list of grids in the workspace.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; gp.workspace = workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Rasters = gp.ListRasters("wsh*","GRID")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Masks = gp.ListRasters("sec*","GRID")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Rasters.Reset()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Masks.Reset()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Raster = Rasters.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Mask = Masks.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Rasters.Sort()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Masks.Sort()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;except:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print "listrasters1"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print gp.GetMessages()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;try: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; while Mask:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print Raster&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print Mask&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Raster = Rasters.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Mask = Masks.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As you can see, both input rasters and mask rasters are in the same geodatabase. I get the result below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;listrasters1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh107&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec101&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh106&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh105&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec102&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh103&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec001&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh102&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec103&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh101&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec105&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec106&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh108&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec107&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh001&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec108&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh109&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec109&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But my goal is to have this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;listrasters1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh001&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec001&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh101&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec101&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh102&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec102&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh103&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec103&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh105&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec105&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh106&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec106&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh107&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec107&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh108&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec108&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wsh109&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sec109&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anyone help me with this? Don't worry about the indents, they got lost while posting this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Alexander Hohl&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;University of North Carolina at Charlotte&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jun 2010 16:57:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-double-loop/m-p/322468#M11359</guid>
      <dc:creator>AlexanderHohl</dc:creator>
      <dc:date>2010-06-25T16:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: Python double loop</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-double-loop/m-p/322469#M11360</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Take the filenames of the rasters and then slice out the number and then iterate over the list of masks and select the one you want by that number.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for raster in Rasters:
 number=raster[3:6]
 for mask in Masks:
&amp;nbsp; if number==mask[3:6]:
&amp;nbsp;&amp;nbsp; DO YOUR STUFF HERE
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print ('No matching mask'); break
 &lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;The reason you arent getting a good sort is the same reason sorting by a text field thats full of numbers doesnt put the numbers in the right order. Text doesnt sort like numbers and your list is all strings. This method doesnt need to be sorted to work. You just match the raster to its mask and go.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:18:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-double-loop/m-p/322469#M11360</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2021-12-11T15:18:50Z</dc:date>
    </item>
  </channel>
</rss>

