簡體   English   中英

您如何計算列表中的最大重復次數?

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

如果我在 Python 中有一個列表,例如

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

如何計算任何元素的最大重復次數? 在這種情況下, 2最多重復 4 次, 1最多重復 3 次。

有沒有辦法做到這一點,而且還記錄最長運行開始的索引?

使用groupby ,它按值對元素進行分組:

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

這是實際的代碼:

>>> 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>)

來自 python 文檔:

groupby() 的操作類似於 Unix 中的 uniq 過濾器。 每次 key 函數的值發生變化時,它都會生成一個 break 或 new group

# [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

如果您還想要最長運行的索引,您可以執行以下操作:

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

循環遍歷列表,跟蹤當前數字、重復次數,並將其與您看到該數字重復的最多次數進行比較。

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

這是我的解決方案:

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]

- 您可以制作列表的新副本,但具有唯一值和相應的命中列表。

- 然后獲取點擊列表的最大值,並從它的索引中獲取您最重復的項目。

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)

但我不知道這是最快的方法還是有更快的解決方案。 如果您認為有更快或更有效的解決方案,請告知我們。

如果您只希望它用於任何元素(即重復次數最多的元素),您可以使用:

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];

這僅計算連續重復( [1,2,2,2,1,2]的結果將是3 )並且僅記錄具有最大數量的元素。

編輯:使 fa 的定義更短...

我編寫此代碼並輕松工作:

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

這段代碼似乎工作:

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

我會使用項目的哈希圖來反擊。

每次看到“鍵”連續時,增加其計數器值。 如果你擊中了一個新元素,請將計數器設置為 1 並繼續前進。 在此線性搜索結束時,您應該獲得每個數字的最大連續計數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM