How to Count the Occurrences of a List Item

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.

count occurrences of list items in second list in python

If you just want to count the number of elements that are in both lists (and you don't need to know how many times they occur in the other list) you can just use:

count = len(set(a).intersection(set(b)))

Or identically:

count = len(set(a) & set(b))

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]

Count occurrences of list item in dataframe column, grouped by another column

We can use DataFrame.explode + crosstab:

# If Not Already a List
# df['words'] = df['words'].str.split(', ')

new_df = df.explode('words')
new_df = pd.crosstab(
new_df['words'], new_df['category']
).reset_index().rename_axis(columns=None)

Or with groupby size + unstack after explode:

new_df = (
df.explode('words') # Explode List into Rows
.groupby(['words', 'category']).size() # Calculate Group Sizes
.unstack(fill_value=0) # Convert Category values to column names
.reset_index().rename_axis(columns=None) # Cleanup
)

or DataFrame.value_counts + unstack after explode:

new_df = (
df.explode('words') # Explode List into Rows
.value_counts() # Count Value Pairs
.unstack(level='category', # Convert Category values to column names
fill_value=0)
.reset_index().rename_axis(columns=None) # Cleanup
)

new_df:

      words  1  2  3
0 cat 3 1 0
1 dog 2 1 0
2 elephant 0 1 2
3 mouse 1 1 0

Setup:

import pandas as pd

df = pd.DataFrame({
'words': [['cat', 'dog', 'dog'], ['cat', 'cat', 'mouse'],
['mouse', 'cat', 'dog', 'elephant'], ['elephant', 'elephant']],
'category': [1, 1, 2, 3]
})

Count occurrences of list element in nested list

This is probably most easily achieved just using a Counter on the first element of each list:

from collections import Counter

c =Counter([l[0] for l in list_all])
list(c.items())

Output:

[('car', 3), ('bicycle', 2), ('taxi', 3), ('motorcycle', 4)]

If you really want a list of lists (as opposed to a list of tuples) in the output, use a list comprehension over c.items():

[list(i) for i in c.items()]

Output:

[['car', 3], ['bicycle', 2], ['taxi', 3], ['motorcycle', 4]]

Counting the frequency of the first element of a list within a list

Just split your task into a two parts:

  1. Retrieve first element from each inner list;
  2. Sort elements in descending order by number of occurrences.

You can easily find an answer of each of this problem on Stack Overflow:

  1. Extract first item of each sublist;
  2. Sorting a List by frequency of occurrence in a list.

Even question you've additionally answered in comments already have been answered: How to access the first element or number in counter using Python.

You should make some research efforts before asking question otherwise you waste both yours time and time of users who will answer your question.

Anyway, let's go back to your problem.

  1. To get first value of inner list you should iterate over outer list and retrieve first element from every item. With simple for loop it will look like this:

    source_list = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], 
    [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]
    first_items_list = []
    for inner_list in source_list:
    first_items_list.append(inner_list[0])

    You can also use list comprehension:

    source_list = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], 
    [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]
    first_items_list = [inner_list[0] for inner_list in source_list]
  2. Now we have to find max element in list of first items by frequency. To not reinvent the wheel you can apply collections.Counter and specifically it's Counter.most_common() method.

    To initialize Counter you need to pass first_items_list generated in code above to Counter() constructor. Then you need to call Counter.most_common() and pass 1 to arguments as you need just most common element:

    from collections import Counter
    ...
    counter = Counter(first_items_list)
    item, number_of_occurrences = counter.most_common(1)[0]

To simplify the code you can change list comprehension to generator expression and pass it directly into Counter constructor:

source = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], 
[1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]
print("Most common:", Counter(inner[0] for inner in source).most_common(1)[0][0])


Related Topics



Leave a reply



Submit