##### Counting number of occurrences of a single digit in a list
 Counting number of occurrences of a single digit in a list python_newbie09 Not Blown Up Yet Posts: 53 Threads: 25 Joined: Jan 2018 Reputation: Aug-07-2019, 09:16 AM My data is as below: [0,1,1,1,1,2,2,2,...85,85,85,80,80,80,...0,1,1,1,2,2,2,....85,85,95,80,80,80,...] I want to be able to count how many times no 85 occurred and in this case it should return twice where I am just interested to know the point where it changes to 85. Using counts, or value_counts will result in the total number of occurrences but that's not what I am looking for. Thanks for the help in advance. Reply fishhook Silly Frenchman Posts: 46 Threads: 0 Joined: Jul 2019 Reputation: Aug-07-2019, 11:02 AM Just make a cycle through the list and check for conditions you need manually. Reply paul18fr Lumberjack Posts: 132 Threads: 35 Joined: Apr 2019 Reputation: Aug-07-2019, 12:17 PM (This post was last modified: Aug-07-2019, 12:17 PM by paul18fr.) is it what you're looking for? (just adapt to your case) ```n = 600; A = np.random.randint(100, size = (n,1), dtype = np.int); loc = np.where(A == 85); loc_exact = list(loc)[0];``` Reply python_newbie09 Not Blown Up Yet Posts: 53 Threads: 25 Joined: Jan 2018 Reputation: Aug-07-2019, 01:04 PM (Aug-07-2019, 12:17 PM)paul18fr Wrote: is it what you're looking for? (just adapt to your case) ```n = 600; A = np.random.randint(100, size = (n,1), dtype = np.int); loc = np.where(A == 85); loc_exact = list(loc)[0];``` Thanks but it doesn't quite achieve what I am looking for. It is tricky because if I adapt this code, the result I will get is 6 occurrences of 85 instead of 2 occurrences. I am still thinking :| Reply ThomasL Minister of Silly Walks Posts: 360 Threads: 5 Joined: Jun 2019 Reputation: Aug-07-2019, 01:12 PM (This post was last modified: Aug-07-2019, 01:18 PM by ThomasL.) (Aug-07-2019, 12:17 PM)paul18fr Wrote: ```n = 600; A = np.random.randint(100, size = (n,1), dtype = np.int); loc = np.where(A == 85); loc_exact = list(loc)[0];```please explain the usage of ; at the end of each line This is python and not javascript! (Aug-07-2019, 01:04 PM)python_newbie09 Wrote: Thanks but it doesn't quite achieve what I am looking for. It is tricky because if I adapt this code, the result I will get is 6 occurrences of 85 instead of 2 occurrences. I am still thinking :|With that code you get the indices where in your list the number 85 occurs. If you only want the first occurence after a bunch of other (smaller) numbers than look at the differences of the indices. If you get e.g. [37,38,39,111,112,114] then 37 and 111 are your numbers. That should be doable. Reply paul18fr Lumberjack Posts: 132 Threads: 35 Joined: Apr 2019 Reputation: Aug-07-2019, 01:26 PM (Aug-07-2019, 01:12 PM)ThomasL Wrote: please explain the usage of ; at the end of each line This is python and not javascript!habit coming from Mathlab (BTW it has no consequence) I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85) Reply ThomasL Minister of Silly Walks Posts: 360 Threads: 5 Joined: Jun 2019 Reputation: Aug-07-2019, 01:31 PM (Aug-07-2019, 01:26 PM)paul18fr Wrote: habit coming from Mathlab (BTW it has no consequence) I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85)a bad habit and yes i know it has no consequence as this is the reason you can code like this if it would not work you would not use ; anymore. ;-) I´m not the threadstarter so i don´t want anything. :-) and i assume the dots should represent a bunch of numbers coming before the 85, so there aren´t any strings in a list of numbers. Reply python_newbie09 Not Blown Up Yet Posts: 53 Threads: 25 Joined: Jan 2018 Reputation: Aug-07-2019, 01:35 PM (Aug-07-2019, 01:26 PM)paul18fr Wrote: (Aug-07-2019, 01:12 PM)ThomasL Wrote: please explain the usage of ; at the end of each line This is python and not javascript!habit coming from Mathlab (BTW it has no consequence) I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85) yes, because the list is very long so I also cannot use the method of just counting the difference as suggested above for 1000 plus rows of time series data. To make it simpler, the list below as an example: n = [0,1,1,1,1,2,2,2,85,85,85,80,80,80,0,1,1,1,2,2,2,85,85,85,80,80,80] I tried to use the code below with the logic of first checking if the value is 85 and if the previous value is not 85 and the next value is the same as 85 then count 1. But I am getting an error; AttributeError: 'int' object has no attribute 'iloc' this code does not include the counting part yet. ```for i, x in enumerate(n): if x==85 and x.loc[i-1]!= 85 and x.iloc[i+1]==85: print(i)``` Reply python_newbie09 Not Blown Up Yet Posts: 53 Threads: 25 Joined: Jan 2018 Reputation: Aug-07-2019, 03:25 PM (Aug-07-2019, 01:35 PM)python_newbie09 Wrote: (Aug-07-2019, 01:26 PM)paul18fr Wrote: habit coming from Mathlab (BTW it has no consequence) I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85) yes, because the list is very long so I also cannot use the method of just counting the difference as suggested above for 1000 plus rows of time series data. To make it simpler, the list below as an example: n = [0,1,1,1,1,2,2,2,85,85,85,80,80,80,0,1,1,1,2,2,2,85,85,85,80,80,80] I tried to use the code below with the logic of first checking if the value is 85 and if the previous value is not 85 and the next value is the same as 85 then count 1. But I am getting an error; AttributeError: 'int' object has no attribute 'iloc' this code does not include the counting part yet. ```for i, x in enumerate(n): if x==85 and x.loc[i-1]!= 85 and x.iloc[i+1]==85: print(i)``` I could probably live with the solution below for now but I need to make sure that it checks until the end of the list. ```a = 0 for i, x in enumerate(n): #print(n[i]) #print(n[i-1]) #print(n[i+1]) if x ==85: #print(n[i]) if n[i-1]!=85: #print(n[i-1]) if n[i+1]==85: #print(n[i+1]) a+=1 print(a)``` Reply paul18fr Lumberjack Posts: 132 Threads: 35 Joined: Apr 2019 Reputation: Aug-07-2019, 05:25 PM (This post was last modified: Aug-07-2019, 05:25 PM by paul18fr.) Is the following what you were looking for at the beginning? Paul ```import numpy as np ##I build a model n = 20 A = [] for i in range(n): A.append("1") A[3] = '...1' A[6] = '...1' ## The list is converted to an array A = np.array(A) loc = np.where(A == "...1") exact_loc = list(loc)[0] # Eureka``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Parse String between 2 Delimiters and add as single list items lastyle 5 619 Apr-11-2021, 11:03 PM Last Post: lastyle Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 346 Feb-05-2021, 03:49 PM Last Post: perfringo convert List with dictionaries to a single dictionary iamaghost 3 637 Jan-22-2021, 03:56 PM Last Post: iamaghost if a string has a digit - print tester_V 2 517 Jan-16-2021, 04:48 AM Last Post: tester_V How to append multiple into a single List ahmedwaqas92 2 585 Jan-07-2021, 08:17 AM Last Post: ahmedwaqas92 Undo interation to make a single list? DustinKlent 2 508 Nov-29-2020, 03:41 AM Last Post: DustinKlent is there a single function to confine a number between two others? Skaperen 7 713 Nov-28-2020, 06:10 PM Last Post: Skaperen Counting Element in Multidimensional List quest_ 1 483 Nov-25-2020, 10:00 PM Last Post: quest_ Counting Number of Element in a smart way quest 2 489 Nov-09-2020, 10:24 PM Last Post: quest Count number of occurrences of list items in list of tuples t4keheart 1 582 Nov-03-2020, 05:37 AM Last Post: deanhystad

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020