Number of events in python array

I have the following array:

a = [0,0,0,1,1,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0]

Every time I have a "1" or a series of them (sequential), this is one event. I need to get in Python how many events my array has. Thus, in this case we will have 5 events (this is 5 times 1 or its sequence). I need to count such events in order to get:

b = [5]

thank

+4
source share
7 answers

You can use itertools.groupby(it does exactly what you want - groups consecutive elements) and count all groups starting with1

In [1]: from itertools import groupby

In [2]: a = [0,0,0,1,1,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0]

In [3]: len([k for k, _ in groupby(a) if k == 1])
Out[3]: 5

what if I wanted to add a condition that an event is given as long as there are 2 or more between them.

, groupby key:

from itertools import groupby


class GrouperFn:
    def __init__(self):
        self.prev = None

    def __call__(self, n):
        assert n is not None, 'n must not be None'

        if self.prev is None:
            self.prev = n
            return n

        if self.prev == 1:
            self.prev = n
            return 1

        self.prev = n
        return n


def count_events(events):
    return len([k for k, _ in groupby(events, GrouperFn()) if k == 1])


def run_tests(tests):
    for e, a in tests:
        c = count_events(e)
        assert c == a, 'failed for {}, expected {}, given {}'.format(e, a, c)

    print('All tests passed')


def main():
    run_tests([
        ([0, 1, 1, 1, 0], 1),
        ([], 0),
        ([1], 1),
        ([0], 0),
        ([0, 0, 0], 0),
        ([1, 1, 0, 1, 1], 1),
        ([0, 1, 1, 0, 1, 1, 0], 1),
        ([1, 0, 1, 1, 0, 1, 1, 0, 0, 1], 2),
        ([1, 1, 0, 0, 1, 1], 2),
        ([0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0], 4)
    ])


if __name__ == "__main__":
    main()

- 0 1, . ( 1), ( 0)

, , , [1, 1, 0, 0] [[1, 1, 0], [0]].

+11

( ):

a = [0,0,0,1,1,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0]
events = (a[0] + a[-1] + sum(a[i] != a[i-1] for i in range(1, len(a)))) / 2
print events

, :)

+2
def num_events(list):
    total = 0
    in_event = 0
    for current in list:
        if current and not in_event:
            total += 1
        in_event = current
    return total

, 1 0, . , 1 .

+1

a = [0,0,0,1,1,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0]

flag = 0
cnt = 0
for j in (a):
    if( j == 1):
        if(flag == 0):
            cnt += 1
        flag = 1
    elif (j == 0):
        flag = 0


print cnt                   
+1

:

sum([(a[i] - a[i-1])>0 for i in range(1, len(a))])
+1

Me - , -)

def get_number_of_events(event_list):
    num_of_events = 0
    for index, value in enumerate(event_list):
        if 0 in (value, index):
            continue
        elif event_list[index-1] == 0:
            num_of_events += 1
    return num_of_events
0
source

To replace multiple occurrences with one occurrence, add each item to the list, unless the last added item is equal to the current one, and complete the count.

def count_series(seq):
    seq = iter(seq)
    result = [next(seq, None)]
    for item in seq:
        if result[-1] != item:
            result.append(item)
    return result

a = [0,0,0,1,1,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0]

count_series(a).count(1)
5
0
source

Source: https://habr.com/ru/post/1649492/


All Articles