Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you calculate the greatest number of repetitions in a list?

Tags:

python

list

If I have a list in Python like

[1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1]

How do I calculate the greatest number of repeats for any element? In this case 2 is repeated a maximum of 4 times and 1 is repeated a maximum of 3 times.

Is there a way to do this but also record the index at which the longest run began?

like image 545
hekevintran Avatar asked May 19 '09 23:05

hekevintran


People also ask

How do you find the maximum occurrence of an element in a list?

Given a list, the task is to find the number of occurrences of the largest element of the list. Method 1: The naive approach is to find the largest element present in the list using max(list) function, then iterating through the list using a for loop and find the frequency of the largest element in the list.

How do I find the most repeated numbers in a list Python?

Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using most_common() method.

How do you count occurrences in a list in Python?

The easiest way to count the number of occurrences in a Python list of a given item is to use the Python . count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned.


2 Answers

Use groupby, it group elements by value:

from itertools import groupby
group = groupby([1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1])
print max(group, key=lambda k: len(list(k[1])))

And here is the code in action:

>>> group = groupby([1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1])
>>> print max(group, key=lambda k: len(list(k[1])))
(2, <itertools._grouper object at 0xb779f1cc>)
>>> group = groupby([1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1, 3, 3, 3, 3, 3])
>>> print max(group, key=lambda k: len(list(k[1])))
(3, <itertools._grouper object at 0xb7df95ec>)

From python documentation:

The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes

# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
# [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D

If you also want the index of the longest run you can do the following:

group = groupby([1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1, 3, 3, 3, 3, 3])
result = []
index = 0
for k, g in group:
   length = len(list(g))
   result.append((k, length, index))
   index += length

print max(result, key=lambda a:a[1])
like image 157
Nadia Alramli Avatar answered Sep 23 '22 11:09

Nadia Alramli


Loop through the list, keep track of the current number, how many times it has been repeated, and compare that to the most times youve seen that number repeated.

Counts={}
Current=0
Current_Count=0
LIST = [1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1]
for i in LIST:
    if Current == i:
        Current_Count++
    else:
        Current_Count=1
        Current=i
    if Current_Count>Counts[i]:
        Counts[i]=Current_Count
print Counts
like image 22
Sparr Avatar answered Sep 22 '22 11:09

Sparr