Python Forum
Counting number of occurrences of a single digit in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting number of occurrences of a single digit in a list
#1
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
#2
Just make a cycle through the list and check for conditions you need manually.
Reply
#3
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
#4
(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
#5
(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
#6
(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
#7
(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
#8
(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
#9
(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
#10
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
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 281 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  prefix ID Number with 0,00 make 3 digit. mg24 1 708 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,758 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,925 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 2,980 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,282 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 1,644 Feb-05-2021, 03:49 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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