Last iteration of a loop

23654
5
Jump to solution
03-21-2013 04:25 AM
Pierre-LucBoivin
Occasional Contributor
Hi,

I would like to get the last iteration of a loop and do a calculate field on it ! I've try to catch the last iteration with this
if index !=len(fclist)-1:   or   if index ==len(fclist)-1:


But still not having a good result

Any suggestions ?


for index, value in enumerate(fclist, start=1):         looptime = time.asctime()         print "Début de la boucle d'intersection en batch:" + looptime         print value                # Intersection                arcpy.Intersect_analysis([IntersectBoucle,value],OutIntersectBoucle)                   print "#### " + str(i) + "ieme intersection(Boucle) ###########"         print "L'intersection est entre : " + str(OutIntersectBoucle)+ " et " + str(value)         fieldnames4 = [f.name for f in arcpy.ListFields(OutIntersectBoucle)]                   # Populate field isSame, Supported and Supporting         arcpy.CalculateField_management(OutIntersectBoucle,"isSame","isSame( !Name!, !Name_1! )","Python_9.3",codeblock)         arcpy.CalculateField_management(OutIntersectBoucle,"Supported","PeuplerZone ( !isSame!, !Name!)","Python_9.3",codeblock2)         arcpy.CalculateField_management(OutIntersectBoucle,"Supporting","PeuplerZone ( !isSame!, !Name_1!)","Python_9.3",codeblock3)               j += 1                           if index !=len(fclist)-1:                 print "La dernière intersection est :" + str(OutIntersectBoucle) +  " + 1"                 arcpy.CalculateField_management(OutIntersectBoucle,"isSame","poly_exc( !isSame!)","Python_9.3",codeblock4)


Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Pierre-LucBoivin
Occasional Contributor
I use a variable to store the count of my list and use it to do catch the last iteration.

lstindex = len(fclist)

View solution in original post

0 Kudos
5 Replies
MichaelVolz
Esteemed Contributor
Are you sure

if index ==len(fclist)-1:

does not work?

This is syntax that I use in my own python script and it captures the last iteration of a loop.

Maybe it is other code that is the problem.
0 Kudos
MathewCoyle
Frequent Contributor
It is because you start your enumerate at 1.
for index, value in enumerate(fclist, start=1):

This means if len(fclist) == 5 then your index will also be 5 at the last iteration whereas with this
if index == len(fclist)-1:

You are capturing the second to last iteration.

Why is this even necessary? You can put your code outside your loop and carry over the values from the last iteration.
0 Kudos
MichaelVolz
Esteemed Contributor
Why is this even necessary? You can put your code outside your loop and carry over the values from the last iteration.

Thanks mzcoyle. With this hint I can cleanup some of my extraneous coding.
0 Kudos
Pierre-LucBoivin
Occasional Contributor
Hi Mathew,

You're right that exactly what was happening I was catching the penultimate element.

Actually, I would like to include it in my loop if it's possible because I have a others operation after updating the last iteration.

i = 1
  
    for index, value in enumerate(fclist, start=1):
        looptime = time.asctime()
        print "Début de la boucle d'intersection en batch:" + looptime
        print value
   
         
         
        # Intersection       
        #arcpy.Intersect_analysis([IntersectBoucle,fclistBoucle],OutIntersectBoucle)
        arcpy.Intersect_analysis([IntersectBoucle,value],OutIntersectBoucle)
         
        print "#### " + str(i) + "ieme intersection(Boucle) ###########"
        print "L'intersection est entre : " + str(OutIntersectBoucle)+ " et " + str(value)
        fieldnames4 = [f.name for f in arcpy.ListFields(OutIntersectBoucle)]
         
        # Peupler les champs isSame, Supported and Supporting
        # Populate field isSame, Supported and Supporting
        arcpy.CalculateField_management(OutIntersectBoucle,"isSame","isSame( !Name!, !Name_1! )","Python_9.3",codeblock)
        arcpy.CalculateField_management(OutIntersectBoucle,"Supported","PeuplerZone ( !isSame!, !Name!)","Python_9.3",codeblock2)
        arcpy.CalculateField_management(OutIntersectBoucle,"Supporting","PeuplerZone ( !isSame!, !Name_1!)","Python_9.3",codeblock3)
     
        j += 1
        
        
        if index ==len(fclist)-1:
                print "La dernière intersection est :" + str(OutIntersectBoucle) +  " + 1"
                arcpy.CalculateField_management(OutIntersectBoucle,"isSame","poly_exc( !isSame!)","Python_9.3",codeblock4)
                 
                 
                 
        # Extraire les enregistrements qui respectent les conditions pour les zones soutenues et supportées
        # Extract feature that meets requirements for Supporting and Supported zones
        arcpy.CopyFeatures_management(OutIntersectBoucle, outIntersect)
         
        # Extract feature that doesn't meets requirements and needs to be Intersect again
        arcpy.MakeFeatureLayer_management(OutIntersectBoucle,IntersectBoucle,isSameQuery)
        arcpy.DeleteField_management(IntersectBoucle,DropField)
        outIntersect = mainFolder + "\\" + "Intersect" + "\\" + "Intersect" + str(j)
        OutIntersectBoucle = "in_memory" + "\\" + "OutIntersect" + str(j)
      
        i += 1


Thanks again
0 Kudos
Pierre-LucBoivin
Occasional Contributor
I use a variable to store the count of my list and use it to do catch the last iteration.

lstindex = len(fclist)
0 Kudos