Populate rows using Update Cursor based on a list of repeating values

656
3
Jump to solution
09-09-2016 08:47 AM
ZacharyHart
Regular Contributor

I've been searching high and low for this (Stackoverflow/exchange, etc, Geonet...). Here it goes. I have a table with a number of records. I have a field called 'Rotation' I would like to populate with a repeating set of values [0,90,180,270]. So imagine the following:

PT_IDROTATION

1

0
290
3180
4270
50
690
7180
8270
90

I've tried to simplify my question as much as possible, but its literally as simple as that. Most of the searches return information related to deleting duplicate/repeated records & values.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

itertools.cycle()

Haven't tested but something like:

>>> from itertools import cycle
>>> rot = cycle((0,90,180,270))
>>> with arcpy.da.UpdateCursor(in_table,["PT_ID","ROTATION"]) as cur:
...     for row in cur:
...         row[1] = next(rot)
...         cur.updateRow(row)

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

itertools.cycle()

Haven't tested but something like:

>>> from itertools import cycle
>>> rot = cycle((0,90,180,270))
>>> with arcpy.da.UpdateCursor(in_table,["PT_ID","ROTATION"]) as cur:
...     for row in cur:
...         row[1] = next(rot)
...         cur.updateRow(row)
ZacharyHart
Regular Contributor

Joshua, this is exactly what I needed, thanks!!!

0 Kudos
DarrenWiens2
MVP Honored Contributor

Go with Joshua's answer, I was just curious to see how hard this would be in Field Calculator (as long as your IDs are reliable):

VB Script (but could do the same in Python): (([PT_ID]-1)*90)-( Int(([PT_ID]/4)-0.25) *360)

0 Kudos