简体   繁体   中英

How do you calculate the greatest number of repetitions in a 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?

Usegroupby , 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])

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

This is my solution:

def longest_repetition(l):
    if l == []:
        return None

    element = l[0]
    new = []
    lar = []

    for e in l:            
        if e == element:
            new.append(e)
        else:
            if len(new) > len(lar):
                lar = new
            new = []
            new.append(e)
            element = e
    if len(new) > len(lar):
        lar = new    
    return lar[0]

-You can make new copy of the list but with unique values and a corresponding hits list.

-Then get the Max of hits list and get from it's index your most repeated item.

oldlist = ["A", "B", "E", "C","A", "C","D","A", "E"]
newlist=[]
hits=[]
for i in range(len(oldlist)):
    if oldlist[i] in newlist:
        hits[newlist.index(oldlist[i])]+= 1
    else:
        newlist.append(oldlist[i])
        hits.append(1);
#find the most repeated item
temp_max_hits=max(hits)
temp_max_hits_index=hits.index(temp_max_hits)
print(newlist[temp_max_hits_index])
print(temp_max_hits)

But I don't know is this the fastest way to do that or there are faster solution. If you think there are faster or more efficient solution, kindly inform us.

If you want it for just any element (ie the element with the most repetitions), you could use:

def f((v, l, m), x):
    nl = l+1 if x==v else 1
    return (x, nl, max(m,nl))

maxrep = reduce(f, l, (0,0,0))[2];

This only counts continuous repetitions (Result for [1,2,2,2,1,2] would be 3 ) and only records the element with the the maximum number.

Edit : Made definition of fa bit shorter ...

i write this code and working easly:

lst = [4,7,2,7,7,7,3,12,57]
maximum=0
for i in lst:
    count = lst.count(i)  
    if count>maximum:
        maximum=count
        indexx = lst.index(i)
print(lst[indexx])

This code seems to work:

l = [1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1]
previous = None

# value/repetition pair
greatest = (-1, -1)
reps = 1

for e in l:
    if e == previous:
        reps += 1
    else:
        if reps > greatest[1]:
            greatest = (previous, reps)

        previous = e
        reps = 1

if reps > greatest[1]:
    greatest = (previous, reps)

print greatest

I'd use a hashmap of item to counter.

Every time you see a 'key' succession, increment its counter value. If you hit a new element, set the counter to 1 and keep going. At the end of this linear search, you should have the maximum succession count for each number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM