Search code examples
pythoniterationcounter

Find the run of elements in an array- python


I have a list here:

a = [1, 1, 0.5, 0.5, 0.25, 0.25, 0.25, 0.25,1,0.25,0.25].

In the list a, I want to print

1 : [0,1]

0.5 : [2,3]

0.25 : [4,5,6,7]

1 : [8,8]

0.25 : [9,10]

What I have tried is this:

import numpy as np
a = [1, 1, 0.5, 0.5, 0.25, 0.25, 0.25, 0.25,1,0.25,0.25]

b = np.array(a).reshape(1,len(a))

x = [1,0.5,0.25]

for v in x:

    c = b - v
    indices = []


##    for i in range(len(c[0])):
##        if c[0,i] == 0.:
##            indices.append(i)
##    print(v,indices)

    i = 0
    
    while i  < len(c[0]):
        if c[0,i] == 0.:
            indices.append(i)
        i = i + 1
        if len(indices) > 1 and indices[-1] != (indices[-2]+1): #len(indices) > 2 and 
            #i = i - 1
            indices = indices[:-1]
            print(i,v,indices)
            indices = []
            
        if len(indices) > 1 and i != (indices[-1]+1):
            print(i,v,indices)
            indices = []

But the output was:

1 [0, 1]

0.5 [2, 3]

0.25 [4, 5, 6, 7]

Can you help me out?


Solution

  • You can write this as a nifty generator function that keeps track of the current run's value and indices:

    def find_runs(a):
        if not a:
            return
        curr_run = None
        for i, v in enumerate(a):
            if not curr_run:
                curr_run = (v, [i])
            elif v == curr_run[0]:
                curr_run[1].append(i)
            else:
                yield curr_run
                curr_run = (v, [i])
        yield curr_run
    
    
    for val, indexes in find_runs([1, 1, 0.5, 0.5, 0.25, 0.25, 0.25, 0.25, 1, 0.25, 0.25]):
        print(val, indexes)
    

    The output is

    1 [0, 1]
    0.5 [2, 3]
    0.25 [4, 5, 6, 7]
    1 [8]
    0.25 [9, 10]