Count consecutive equal values in array [duplicate]

2024/10/10 9:16:40

Say I have the following numpy array:

a = np.array([1,5,5,2,3,6,5,2,5,5,5])

I'm trying to come up with a numpy solution to count the amount of times a given value appears consecutively. So, for example for number 5 I'd like to get:

array([2,1,3])

As it appears consecutively 3 times in the array, with the specified amount of repetitions on each time.

Answer

Here is one option adapted from this answer:

def count_consecutive(arr, n):# pad a with False at both sides for edge cases when array starts or ends with nd = np.diff(np.concatenate(([False], arr == n, [False])).astype(int))# subtract indices when value changes from False to True from indices where value changes from True to Falsereturn np.flatnonzero(d == -1) - np.flatnonzero(d == 1)count_consecutive(a, 5)
# array([2, 1, 3])
https://en.xdnf.cn/q/118472.html

Related Q&A

how can I show please wait gif image before the process is complete

I want to show "please wait gif" image from img() class before the ListApp() class process is complete and then as soon as the process of that class is completed the screeen of ListApp should…

TypeError: list of indices must be integers, not str

What is wrong in my code to give me the error:TypeError: List of indices must be integers, not strHere is my code:print("This programe will keep track of your TV schedule.") Finish = False Sh…

Assignment in conditional not permitted in Python?

Why is code like if a = "hello":passinvalid in Python? The a = "Hello" is just a expression whose value is the Rvalue. Its valid in most languages like C or php. Some opinions?

Django - Join two Table without Foreign key

I have two tables and want to join them.. but I cant do that without rawQueryset and raw SQL. how can i join two models without foreign key? The columns for JOIN is not unique so it cant be PK and For…

Understanding lambda functions

Well I did try to read about Lambda functions but did not get across any link which explains few questions about its flow and the way it is handled by python interpretor or may be I could not understan…

JSON to Python dataframe: mapping values from another API

I have an API with student data like this, for every student id there will be a corresponding API link with mark details. for example: https://api.school.com/2020/students.json {"Students": […

Create a cycle out of scattered points

I know this sounds trivial, but my head is refusing to give an algorithm for this.I have a bunch of points scattered on a 2-D plane and want to store them in a list such that they create a ring. The po…

Python Dictionary w/ 2 Keys?

Can I have a python dictionary with 2 same keys, but with different elements?

Tkinter throwing a KeyError when trying to change frames

Im learning tkinter off of the Sentdex tutorials and I into a problem when trying to change pages. My compiler throws something about a KeyError that it doesnt give whenever I change the button on the …

How to send messages to other clients only in the sequence of adding clients?

https://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/sever.pyhttps://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/client.pyWhen server starts, and suppose three clients …