<?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: How to use SequentialNumber in For Loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080512#M61819</link>
    <description>&lt;P&gt;Hi Joe,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for responding. I needed to properly quote the code snip into the calculate field call. I used triple quotes as shown below to get the code to work.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for cable in myValues:
    arcpy.management.SelectLayerByAttribute(fiber, "NEW_SELECTION", "cable_name = '%s'"                         % cable, None)
    arcpy.management.CalculateField(fiber, "SegmentID", "SequentialNumber()", "PYTHON3", """
rec=0 
def SequentialNumber():
    global rec
    pStart = 1
    pInterval = 1
    if (rec == 0):
        rec = pStart
    else:
        rec = rec + pInterval
    return rec """
,'TEXT')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;</description>
    <pubDate>Mon, 19 Jul 2021 20:52:05 GMT</pubDate>
    <dc:creator>ChrisGAEG</dc:creator>
    <dc:date>2021-07-19T20:52:05Z</dc:date>
    <item>
      <title>How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080489#M61817</link>
      <description>&lt;P&gt;I am trying to apply Sequential numbering in groups via a for loop. The loop should select a group of rows, apply sequential number to just those rows in the group, then move on to the next selected group. Here's the code I was trying to use in the Python Window in Pro.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for cable in myValues:
    arcpy.management.SelectLayerByAttribute(fiber, "NEW_SELECTION", "cable_name = '%s'"% cable, None)
    arcpy.management.CalculateField(fiber, "SegmentID", "SequentialNumber()", "PYTHON3", 
    rec=0
    def SequentialNumber():
        global rec
        pStart = 1
        pInterval = 1
        if (rec == 0):
            rec = pStart
        else:
            rec = rec + pInterval
        return rec
,'TEXT')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The process works manually, but I'm not sure what I'm missing. I think the issue is formatting the code block of the SequentialNumber function within the Calculate Attributes run inside the for loop. Any help on this is greatly appreciated!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:30:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080489#M61817</guid>
      <dc:creator>ChrisGAEG</dc:creator>
      <dc:date>2021-07-19T20:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080508#M61818</link>
      <description>&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;The loop should select a group of rows....&amp;nbsp;&amp;nbsp;then move on to the next selected group&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;What is your selection criteria?&amp;nbsp; I don't see it in your code sample.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;The process works manually...&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;And what doesn't work?&amp;nbsp; Are there errors?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:49:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080508#M61818</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-07-19T20:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080512#M61819</link>
      <description>&lt;P&gt;Hi Joe,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for responding. I needed to properly quote the code snip into the calculate field call. I used triple quotes as shown below to get the code to work.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for cable in myValues:
    arcpy.management.SelectLayerByAttribute(fiber, "NEW_SELECTION", "cable_name = '%s'"                         % cable, None)
    arcpy.management.CalculateField(fiber, "SegmentID", "SequentialNumber()", "PYTHON3", """
rec=0 
def SequentialNumber():
    global rec
    pStart = 1
    pInterval = 1
    if (rec == 0):
        rec = pStart
    else:
        rec = rec + pInterval
    return rec """
,'TEXT')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:52:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080512#M61819</guid>
      <dc:creator>ChrisGAEG</dc:creator>
      <dc:date>2021-07-19T20:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080516#M61820</link>
      <description>&lt;P&gt;maybe&lt;/P&gt;&lt;LI-CODE lang="python"&gt;code_block = """
rec=0
def SequentialNumber():
    global rec
    pStart = 1
    pInterval = 1
    if (rec == 0):
        rec = pStart
    else:
        rec = rec + pInterval
    return rec
"""
for cable in myValues:
    arcpy.management.SelectLayerByAttribute(fiber, "NEW_SELECTION", "cable_name = '%s'"% cable, None)

    arcpy.management.CalculateField(fiber, "SegmentID", "SequentialNumber()", "PYTHON3", code_block, 'TEXT')&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:56:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080516#M61820</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-07-19T20:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080571#M61821</link>
      <description>&lt;P&gt;What about using&amp;nbsp;&lt;A href="https://docs.python.org/3/library/functions.html#enumerate" target="_self"&gt;enumerate()&lt;/A&gt; to get sequential numbers in a loop.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for enum, cable in enumerate(myValues):
    arcpy.management.SelectLayerByAttribute(fiber, "NEW_SELECTION", f"cable_name = '{cable}'", None)
    arcpy.management.CalculateField(
        in_table=fiber,
        field="SegmentID",
        expression=enum,
        expression_type="PYTHON3"
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 19 Jul 2021 22:04:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1080571#M61821</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-07-19T22:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1081257#M61833</link>
      <description>&lt;P&gt;Thank you Dan, this is another good option. I had one follow up question about this code. Is there a way to reference a value in the table as a variable and assign pStart to that value? For instance if the value in the first cell in a group of 5 rows was 5, the code would enumerate from 5 - 9 for that group.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 12:48:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1081257#M61833</guid>
      <dc:creator>ChrisGAEG</dc:creator>
      <dc:date>2021-07-21T12:48:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1081414#M61839</link>
      <description>&lt;P&gt;Chris, you would be better off using search and update cursors.&amp;nbsp; Calculate field is a shortcut to these and for small/simple expressions.&lt;/P&gt;&lt;P&gt;I would normally use numpy and group entries accordingly, then do the sequential entries from there.&amp;nbsp; arcpy's ExtendTable is then used to bring the resultant column back into the featureclass table.&lt;/P&gt;&lt;P&gt;Panda' (groupby) has similar functionality&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 16:47:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1081414#M61839</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-07-21T16:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SequentialNumber in For Loop</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1082653#M61857</link>
      <description>&lt;P&gt;Dan,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the advice. I ended up using Pandas.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 12:42:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-sequentialnumber-in-for-loop/m-p/1082653#M61857</guid>
      <dc:creator>ChrisGAEG</dc:creator>
      <dc:date>2021-07-26T12:42:54Z</dc:date>
    </item>
  </channel>
</rss>

