Calculate repeating values in field

2256
12
Jump to solution
10-13-2017 03:48 PM
JeffBaker2
New Contributor III

Anyone have any ideas how to calculate a field so the values (going down the table) are

1

2

3

1

2

3

1

2

3

etc. to the end of the table?

These values will end up being color codes for rendering.  The actual values will be from 0-9, but need some way to get the repeating values in.  Another option might be to calculate random values instead of repeating values.  Any thoughts?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

This passes the current field value to the function (not what you want - would pass all 0s first time, then all 1s, etc.):

seq_num_max(!ColorCode!)‍

Try (you always want 3):

seq_num_max(3)‍‍

And code block (untested):

cnt = 0
def seq_num_max(val):
    global cnt
    if cnt < val:
        cnt += 1
    else:
        cnt = 0
    return cnt

Or (untested):

cnt = -1
def seq_num_max(val):
  global cnt
  cnt += 1
  return (cnt % val) + 1

View solution in original post

12 Replies
DanPatterson_Retired
MVP Emeritus
cnt = 0
def seq_num_max(val):
    global cnt
    if cnt < val:
        cnt += 1
    else:
        cnt = 0
    return cnt

# in expression box ... seq_num_max(3) ... will number from 0 to 3 before repeating

will get you started ... for use in the field calculator obviously

MicahBabinski
Occasional Contributor III

I think this would do it:

In field calculator Codeblock (Python):

def Calculate(repeatingValue):
    interval = 1
    if cntr < 3:
        cntr += interval
    else:
        cntr = 1
    return cntr‍‍‍‍‍‍‍‍‍

Then in your expression it would be Calculate(!YourFieldName!)

Micah

DarrenWiens2
MVP Honored Contributor
for i in range(10):
    print (i % 3) + 1

1
2
3
1
2
3
1
2
3
1
JeffBaker2
New Contributor III

Thanks for your replies.  Dan, Micah, each of your code pieces seems to run well, but the values are not populating in the highlighted field.  All rows remain Null--see below.  Ideas?

0 Kudos
MicahBabinski
Occasional Contributor III

Hey Jeff,

You'll need to apply that function in the main calc expression box:

seq_num_max(!ColorCode!)

That should do it.

JeffBaker2
New Contributor III

I am now getting all zero's in the ColorCode field.  Am I setting up the Field Calculator dialog correctly?  Or maybe something else?

0 Kudos
JeffBaker2
New Contributor III

A little more info now . . . 

Each time I run this code:

cnt = 0
def seq_num_max(val):
    global cnt
    if cnt < val:
        cnt += 1
    else:
        cnt = 0
    return cnt

The result calculates ALL values to 0, then to 1, then to 2, then 3, then back to 0 again, and so on.  Still not sure how to get the repeating values?

0 Kudos
DarrenWiens2
MVP Honored Contributor

This passes the current field value to the function (not what you want - would pass all 0s first time, then all 1s, etc.):

seq_num_max(!ColorCode!)‍

Try (you always want 3):

seq_num_max(3)‍‍

And code block (untested):

cnt = 0
def seq_num_max(val):
    global cnt
    if cnt < val:
        cnt += 1
    else:
        cnt = 0
    return cnt

Or (untested):

cnt = -1
def seq_num_max(val):
  global cnt
  cnt += 1
  return (cnt % val) + 1
JeffBaker2
New Contributor III

And Darren hits the grand slam . . . that worked!  Thank you!

You gave two suggestions--the second one did the trick.  

Thanks to all who shared your ideas.

0 Kudos