Questions tagged [counting]

Counting is the action of finding the number of elements of a finite set of objects.

Counting is the action of finding the number of elements of a finite set of objects. The traditional way of counting consists of continually increasing a (mental or spoken) counter by a unit for every element of the set, in some order, while marking (or displacing) those elements to avoid visiting the same element more than once, until no unmarked elements are left; if the counter was set to one after the first object, the value after visiting the final object gives the desired number of elements. The related term enumeration refers to uniquely identifying the elements of a finite (combinatorial) set or infinite set by assigning a number to each element.

Counting using tally marks at Hanakapiai Beach Counting sometimes involves numbers other than one; for example, when counting money, counting out change, "counting by twos" (2, 4, 6, 8, 10, 12, ...), or "counting by fives" (5, 10, 15, 20, 25, ...). There is archeological evidence suggesting that humans have been counting for at least 50,000 years. Counting was primarily used by ancient cultures to keep track of social and economic data such as number of group members, prey animals, property, or debts (i.e., accountancy). The development of counting led to the development of mathematical notation, numeral systems, and writing.

1837 questions
201
votes
8 answers

Counting the number of True Booleans in a Python List

I have a list of Booleans: [True, True, False, False, False, True] and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3.) I have found examples of looking for the number of…
acs
  • 2,089
  • 3
  • 15
  • 10
136
votes
15 answers

Count and display occurrences of unique values in an array

I am working with a one dimensional array in PHP. I would like to detect the presence of duplicate values, then count the number of duplicate values and output the results. For example, given the following array: $array = [ 'apple', …
mikey_w
  • 1,747
  • 3
  • 13
  • 12
82
votes
4 answers

How do I count the letters in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch?

How do I count the letters in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch? print(len('Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')) Says 58 Well if it was that easy I wouldn't be asking you, now would I?! Wikipedia…
Madarch
  • 831
  • 4
  • 5
75
votes
14 answers

Find the item with maximum occurrences in a list

In Python, I have a list: L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 56, 6, 7, 67] I want to identify the item that occurred the highest number of times. I am able to solve it but I need the fastest way to do so. I know there is a nice…
zubinmehta
  • 4,368
  • 7
  • 33
  • 51
69
votes
14 answers

Item frequency count in Python

Assume I have a list of words, and I want to find the number of times each word appears in that list. An obvious way to do this is: words = "apple banana apple strawberry banana lemon" uniques = set(words.split()) freqs = [(item,…
Daniyar
  • 1,680
  • 2
  • 15
  • 23
65
votes
19 answers

Finding the number of digits of an integer

What is the best method to find the number of digits of a positive integer? I have found this 3 basic methods: conversion to string String s = new Integer(t).toString(); int len = s.length(); for loop for(long long int temp = number; temp >=…
daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77
59
votes
6 answers

LogLog and HyperLogLog algorithms for counting of large cardinalities

Where can I find a valid implementation of LogLog algorithm? Have tried to implement it by myself but my draft implementation yields strange results. Here it is: function LogLog(max_error, max_count) { function log2(x) { return…
actual
  • 2,370
  • 1
  • 21
  • 32
50
votes
9 answers

python histogram one-liner

There are many ways to write a Python program that computes a histogram. By histogram, I mean a function that counts the occurrence of objects in an iterable and outputs the counts in a dictionary. For example: >>> L = 'abracadabra' >>>…
mykhal
  • 19,175
  • 11
  • 72
  • 80
48
votes
1 answer

SQLite count, group and order by count

I have a table that looks like this: FOO BAR BAZ ----+----+---- foo1 bar1 baz1 foo2 bar3 baz2 foo3 bar1 baz3 foo4 bar1 baz4 foo5 bar3 baz5 foo6 bar1 baz6 foo7 bar2 baz7 And as a result I would like to get the count of how many times each bar…
tkit
  • 8,082
  • 6
  • 40
  • 71
47
votes
4 answers

What STL algorithm can determine if exactly one item in a container satisfies a predicate?

I need an STL algorithm that takes a predicate and a collection and returns true if one and only one member of the collection satisfies the predicate, otherwise returns false. How would I do this using STL algorithms? E.g., to replace the following…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
44
votes
12 answers

How to count digits, letters, spaces for a string in Python?

I am trying to make a function to detect how many digits, letter, spaces, and others for a string. Here's what I have so far: def count(x): length = len(x) digit = 0 letters = 0 space = 0 other = 0 for i in x: if…
ray
  • 641
  • 2
  • 8
  • 13
39
votes
2 answers

How can we track hashtags with the new facebook hashtag implementation

Since Facebook introduced hashtags, I've been interested in finding out more about them. Can someone point me in the direction of possibly tracking the hashtags similar to how twitter allows us access to pull hashtag data via their API. I can count…
Shawn Mohammed
  • 391
  • 1
  • 3
  • 3
37
votes
5 answers

count the number of occurrences of a certain value in a dictionary in python?

If I have got something like this: D = {'a': 97, 'c': 0 , 'b':0,'e': 94, 'r': 97 , 'g':0} If I want for example to count the number of occurrences for the "0" as a value without having to iterate the whole list, is that even possible and how?
RowanX
  • 1,272
  • 2
  • 14
  • 27
32
votes
8 answers

How to determine if an array has any elements or not?

How do I find if an array has one or more elements? I need to execute a block of code where the size of the array is greater than zero. if ($result > 0) { // Here is the code body which I want to execute } else { // Here is some other…
Sajid Mehmood
  • 483
  • 2
  • 6
  • 18
31
votes
5 answers

Counting regex pattern matches in one line using sed or grep?

I want to count the number of matches there is on one single line (or all lines as there always will be only one line). I want to count not just one match per line as in echo "123 123 123" | grep -c -E "123" # Result: 1 Better example: echo "1 1 2…
Tyilo
  • 28,998
  • 40
  • 113
  • 198
1
2 3
99 100