Python Forum
Counting number of occurrences of a single digit in a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Counting number of occurrences of a single digit in a list (/thread-20368.html)

Pages: 1 2


Counting number of occurrences of a single digit in a list - python_newbie09 - Aug-07-2019

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.


RE: Counting number of occurrences of a single digit in a list - fishhook - Aug-07-2019

Just make a cycle through the list and check for conditions you need manually.


RE: Counting number of occurrences of a single digit in a list - paul18fr - Aug-07-2019

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];



RE: Counting number of occurrences of a single digit in a list - python_newbie09 - Aug-07-2019

(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 :|


RE: Counting number of occurrences of a single digit in a list - ThomasL - Aug-07-2019

(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.


RE: Counting number of occurrences of a single digit in a list - paul18fr - Aug-07-2019

(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)


RE: Counting number of occurrences of a single digit in a list - ThomasL - Aug-07-2019

(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.


RE: Counting number of occurrences of a single digit in a list - python_newbie09 - Aug-07-2019

(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)



RE: Counting number of occurrences of a single digit in a list - python_newbie09 - Aug-07-2019

(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)



RE: Counting number of occurrences of a single digit in a list - paul18fr - Aug-07-2019

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