A Method to Count Occurrences in a List

How do I count the occurrences of a list item?

If you only want a single item's count, use the count method:

>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3


Important: this is very slow if you are counting multiple different items

Each count call goes over the entire list of n elements. Calling count in a loop n times means n * n total checks, which can be catastrophic for performance.

If you want to count multiple items, use Counter, which only does n total checks.

A method to count occurrences in a list

How about something like this ...

var l1 = new List<int>() { 1,2,3,4,5,2,2,2,4,4,4,1 };

var g = l1.GroupBy( i => i );

foreach( var grp in g )
{
Console.WriteLine( "{0} {1}", grp.Key, grp.Count() );
}

Edit per comment: I will try and do this justice. :)

In my example, it's a Func<int, TKey> because my list is ints. So, I'm telling GroupBy how to group my items. The Func takes a int and returns the the key for my grouping. In this case, I will get an IGrouping<int,int> (a grouping of ints keyed by an int). If I changed it to (i => i.ToString() ) for example, I would be keying my grouping by a string. You can imagine a less trivial example than keying by "1", "2", "3" ... maybe I make a function that returns "one", "two", "three" to be my keys ...

private string SampleMethod( int i )
{
// magically return "One" if i == 1, "Two" if i == 2, etc.
}

So, that's a Func that would take an int and return a string, just like ...

i =>  // magically return "One" if i == 1, "Two" if i == 2, etc. 

But, since the original question called for knowing the original list value and it's count, I just used an integer to key my integer grouping to make my example simpler.

How do you count occurrences in a list in Python?

As recommended above, the Counter class from the collections module is definitely the way to go for counting applications.

This solution also addresses the request to count words in multiple files using the fileinput.input() method to iterate over the contents of all the filenames specified on the command line (or if no filenames specified on the command line then will read from STDIN, typically the keyboard)

Finally it uses a little more sophisticated approach for breaking the line into 'words' with a regular expression as a delimiter. As noted in the code it will handle contractions more gracefully (however it will be confused by apostrophes being used a single quotes)

"""countwords.py
count all words across all files
"""

import fileinput
import re
import collections

# create a regex delimiter that is any character that is not 1 or
# more word character or an apostrophe, this allows contractions
# to be treated as a word (eg can't won't didn't )
# Caution: this WILL get confused by a line that uses apostrophe
# as a single quote: eg 'hello' would be treated as a 7 letter word

word_delimiter = re.compile(r"[^\w']+")

# create an empty Counter

counter = collections.Counter()

# use fileinput.input() to open and read ALL lines from ALL files
# specified on the command line, or if no files specified on the
# command line then read from STDIN (ie the keyboard or redirect)

for line in fileinput.input():
for word in word_delimiter.split(line):
counter[word.lower()] += 1 # count case insensitively

del counter[''] # handle corner case of the occasional 'empty' word

# compute the total number of words using .values() to get an
# generator of all the Counter values (ie the individual word counts)
# then pass that generator to the sum function which is able to
# work with a list or a generator

total = sum(counter.values())

# iterate through the key/value pairs (ie word/word_count) in sorted
# order - the lambda function says sort based on position 1 of each
# word/word_count tuple (ie the word_count) and reverse=True does
# exactly what it says = reverse the normal order so it now goes
# from highest word_count to lowest word_count

print("{:>10s} {:>8s} {:s}".format("occurs", "percent", "word"))

for word, count in sorted(counter.items(),
key=lambda t: t[1],
reverse=True):
print ("{:10d} {:8.2f}% {:s}".format(count, count/total*100, word))

Example output:

$ python3 countwords.py
I have a dog, he is a good dog, but he can't fly
^D

occurs percent word
2 15.38% a
2 15.38% dog
2 15.38% he
1 7.69% i
1 7.69% have
1 7.69% is
1 7.69% good
1 7.69% but
1 7.69% can't
1 7.69% fly

And:

$ python3 countwords.py text1 text2
occurs percent word
2 11.11% hello
2 11.11% i
1 5.56% there
1 5.56% how
1 5.56% are
1 5.56% you
1 5.56% am
1 5.56% fine
1 5.56% mark
1 5.56% where
1 5.56% is
1 5.56% the
1 5.56% dog
1 5.56% haven't
1 5.56% seen
1 5.56% him

Java: How to count the occurrences in a ArrayList with a method

nums.contains(datum) returns true if the list contains an element that is equal to datum. You want to check each element though.

Change:

if(nums.contains(datum));  // Note: ";" should be deleted too!

to:

if (nums.get(i).equals(datum))

Note that you had a trailing ; which means your if does nothing if its condition is true.

However, it is easier and more idiomatic to use a foreach loop:

for (int i : nums) {
if (datum == i)
count++;
}

And of course there's the one line version based on a stream:

return (int)nums.stream().filter(i -> i == datum).count();

How to count occurrences of specific element for arrays in a list?

One way is to use Counter

In [3]: from collections import Counter

Gives frequencies of all numbers

In [4]: [Counter(x) for x in a]
Out[4]: [Counter({2: 3, 1: 1}), Counter({1: 1, 3: 1})]

To get count for only 2

In [5]: [Counter(x)[2] for x in a]
Out[5]: [3, 0]

Alternatively, you could use np.bincount method, to count number of occurrences of each value in array of non-negative ints.

In [6]: [np.bincount(x) for x in a]
Out[6]: [array([0, 1, 3], dtype=int64), array([0, 1, 0, 1], dtype=int64)]

Extract counts for number 2

In [7]: [np.bincount(x)[2] for x in a]
Out[7]: [3, 0]

How to count the frequency of the elements in an unordered list?

If the list is sorted, you can use groupby from the itertools standard library (if it isn't, you can just sort it first, although this takes O(n lg n) time):

from itertools import groupby

a = [5, 1, 2, 2, 4, 3, 1, 2, 3, 1, 1, 5, 2]
[len(list(group)) for key, group in groupby(sorted(a))]

Output:

[4, 4, 2, 1, 2]

Pythonic way to count occurrences from a list in a string

This works!

def occurrence_counter(target_string):
return sum(map(lambda x: x in string_list, target_string.split(' ')))

The string gets split into tokens, then each token gets transformed into a 1 if it is in the list, a 0 otherwise. The sum function, at last, sums those values.

EDIT: also:

def occurrence_counter(target_string):
return len(list(filter(lambda x: x in string_list, target_string.split(' '))))


Related Topics



Leave a reply



Submit