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.
My code examples are always for Python >=3.6.0
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
  Parse String between 2 Delimiters and add as single list items lastyle 5 779 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 420 Feb-05-2021, 03:49 PM
Last Post: perfringo
  convert List with dictionaries to a single dictionary iamaghost 3 787 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  if a string has a digit - print tester_V 2 601 Jan-16-2021, 04:48 AM
Last Post: tester_V
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 698 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Undo interation to make a single list? DustinKlent 2 616 Nov-29-2020, 03:41 AM
Last Post: DustinKlent
  is there a single function to confine a number between two others? Skaperen 7 850 Nov-28-2020, 06:10 PM
Last Post: Skaperen
  Counting Element in Multidimensional List quest_ 1 593 Nov-25-2020, 10:00 PM
Last Post: quest_
  Counting Number of Element in a smart way quest 2 560 Nov-09-2020, 10:24 PM
Last Post: quest
  Count number of occurrences of list items in list of tuples t4keheart 1 700 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