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
#11
If the data is very big (does not fit in memory or does not fit on your hard drive,
you often have with frameworks a kind of generator or "magic" iterator, which
loads new data in chunks.

from collections import namedtuple


CountIndexResult = namedtuple('CountIndexResult', 'count index')


def count(iterable_or_gen, value):
    first_index = None
    count = 0
    for index, element in enumerate(iterable_or_gen):
        if element == value:
            if first_index is None:
                first_index = index
            count +=1
    return CountIndexResult(count, first_index)


count([2,2,2,2,2,2,1,1,1,1,1,1,2,2], 1)
Output:
CountIndexResult(count=6, index=6)
If your tuple/list has lesser than one million elements, you can use the methods count and index.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
(Aug-07-2019, 06:49 PM)DeaD_EyE Wrote: If the data is very big (does not fit in memory or does not fit on your hard drive,
you often have with frameworks a kind of generator or "magic" iterator, which
loads new data in chunks.

from collections import namedtuple


CountIndexResult = namedtuple('CountIndexResult', 'count index')


def count(iterable_or_gen, value):
    first_index = None
    count = 0
    for index, element in enumerate(iterable_or_gen):
        if element == value:
            if first_index is None:
                first_index = index
            count +=1
    return CountIndexResult(count, first_index)


count([2,2,2,2,2,2,1,1,1,1,1,1,2,2], 1)
Output:
CountIndexResult(count=6, index=6)
If your tuple/list has lesser than one million elements, you can use the methods count and index.

Thank you. I will try this as well.
Reply
#13
If I understand the task at hand correctly then groupby can be used:

>>> data = [0,1,1,1,1,2,2,2,85,85,85,80,80,80,0,1,1,1,2,2,2,85,95,80,80,80]
>>> [[*group][0][0] for match, group in groupby(enumerate(data), key=lambda digit: digit[1] == 85) if match == True]
[8, 21]   # index of first 85 in group
EDIT:
If groups are large then indices can be obtained without unpacking next(group)[0]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 286 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,013 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,763 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,926 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,285 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,647 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