Counting consecutive numbers in a list

10420
21
03-22-2019 07:33 AM
JasonWilder
New Contributor III

Table Mock-Up

Hi there. I need help assembling a python script that counts the number of consecutive numbers in a Field (Long Int) and writes that count in another field for those numbers. The field comes sorted appropriately.The image is what I am trying to accomplish. I cobbled together an Update Cursor, but having a hard time integrating the counting code into it. Any help is much appreciated.

import arcpy

fc = r'C:\Users\a19573\Desktop\Test\Test.gdb\TestCount'
fields = ['Consecutive','Count']

count_values = {}

with arcpy.da.UpdateCursor (fc, fields) as cursor:
    for row in cursor:
        if not row[0] in count_values.keys():
            count_values[row[0]] = 1
        else:
            count_values[row[0]] += 1
        row[1] = count_values[row[0]]
        cursor.updateRow(row)

### Test Code to integrate into Update Cursor above
random_list=[1,4,5,6,7,9,9,19,21,22,23,24] # Set of consecutive numbers in Field 'Consecutive'
def count_consec(lst):
    consec = [1]
    for x, y in zip(lst, lst[1:]):
        if x == y - 1:
            consec[-1] += 1
        else:
            consec.append(1)
    return consec
print(count_consec(random_list))
0 Kudos
21 Replies
Arne_Gelfert
Occasional Contributor III

So which part of this is not working? Looks like you're trying to tackle this 2 ways.

0 Kudos
JasonWilder
New Contributor III

Hi. Both code blocks work independently of each other. I don't understand how to get my desired result using the return from def count_consec into the update row cursor in order to write the count of consecutive numbers into the Count field. Probably  missing something fundamental. My gut is telling me the count code in the update cursor code block needs to be simplified and rewritten with the code from the def. I appreciate the reply.

0 Kudos
Arne_Gelfert
Occasional Contributor III

Oh, I think, I get it... you don't want to be populating the 'Count' field with the current count, 1 for the 1st, 2 for the 2nd, but with the total count of consecutive numbers. So if there are 7 55's in field 1, field 2 should have the value 7 for all 55's. Is that it?

0 Kudos
JasonWilder
New Contributor III

Yep! The field comes in to me in a specific way, so I don't sort the field or count all instances of specific number. I think about it like making subsets for each range of consecutive numbers the code finds going down the column, then counting how many numbers are in the subset. For example; starting at the first row: 1,2,3,4 (first subset has 4 consecutive), the next number in the sequence is 8 (second subset with only 1 consecutive), next numbers are 34,35,36,37,38 (third subset with 5), next numbers may be 1,2,3,4 again (fourth subset has 4 consecutive), and so on down the rows. Then, the 'Count' field would contain the corresponding count of the subset.

0 Kudos
Arne_Gelfert
Occasional Contributor III

Ok, still different from what I understood But I think I get it now.

0 Kudos
DanPatterson_Retired
MVP Emeritus
So to clarify, is this what you are looking for?

      [[ 0, 54,  6,  0,  6],   
       [ 1, 34,  3,  6,  9],
       [ 2, 52,  2,  9, 11],
       [ 3, 21,  2, 11, 13],
       [ 4, 55,  1, 13, 14],
       [ 5, 52,  2, 14, 16],
       [ 6, 53,  1, 16, 17],
       [ 7, 55,  1, 17, 18],
       [ 8, 35,  3, 18, 21],
       [ 9, 53,  1, 21, 22]])

for '0'  first sequence starts at 54, is six values long and  begins at index 0 to 6
    '1'  second sequence begins at 34, is 3 long from the 6 to 9 value in the original
    '2'  third sequence 2 long, from the 9 to 11 in the original
    etc
0 Kudos
JasonWilder
New Contributor III

Hi Dan. Thanks for the reply! I like the numpy approach as it simplifies things quite a bit, but don't think the return is precisely what I need. I should clarify, the random_list was something I was playing with to test the def count_consec code. The def returns counts as I would expect from any list with or without consecutive numbers. Now just figuring out how to leverage all or part of that def or potentially numpy into the Update Cursor code.

0 Kudos
JasonWilder
New Contributor III

Hi Dan. I didn't see your explanation at first in your code block when I was reading it. Let me think about it more. It may be what I need.

0 Kudos
JasonWilder
New Contributor III

Hey Dan. I got study your output a bit more and yes, I think this is what I am after actually. Just need to capture the number in the third column of that array and use it in the cursor so I can write it to the 'Count' field. Any hints on how to do that?

0 Kudos