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
#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


Messages In This Thread
RE: Counting number of occurrences of a single digit in a list - by python_newbie09 - Aug-07-2019, 03:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] a 4-digit display vr34 2 452 Apr-12-2024, 05:38 PM
Last Post: vr34
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 476 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Delete strings from a list to create a new only number list Dvdscot 8 1,761 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,602 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  prefix ID Number with 0,00 make 3 digit. mg24 1 828 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,079 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,470 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,265 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,330 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,534 Apr-11-2021, 11:03 PM
Last Post: lastyle

Forum Jump:

User Panel Messages

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