undefinedfix
Sign in

Calculate the maximum number of consecutive repetitions of a value in the list?

edneh edited in Tue, 10 May 2022
s = [-175, -842, -187, -66, -1, -1, -148, -17, -32, -35, 35, -1, -1, -1, -278, 278, -432, -92, -25, 549, -666, -45, -547, -38, -7, -101, -28, -43, -31, -4, 1510, -1958, -40, -25, -7, -1, -1, -1, -1, -15]

The data example is as above. You want to calculate the number of times that - 1 repeats the most (in the example, the number of times that - 1 repeats the most is 4). Post my code:

#这里把 -1 的索引计算出来
nn = []
j = 0
for i in s:
    if i == -1:
        nn.append(j)
        j += 1
    else:
        j += 1
        continue
print(nn)

#根据前面计算出的索引,看看索引位置前的数值是否为- 1 
jj = []
for i in nn:
    j = 1
    print(i)
    print(i + j)
    if s[i] == -1 and s[i + j] == -1:
        jj.append(i+j)
print(jj)
5 Replies
lucky1928
commented on Wed, 11 May 2022

I can't run without temper

s = [-175, -842, -187, -66, -1, -1, -148, -17, -32, -35, 35, -1, -1, -1, -278, 278, -432, -92, -25,
     549, -666, -45, -547, -38, -7, -101, -28, -43, -31, -4, 1510, -1958, -40, -25, -7, -1, -1, -1, -1, -15]
Max = 0 # 已知最大连续出现次数初始为0
cur_repeat_time = 1 # 记录当前元素是第几次连续出现
pre_element = None  # 记录上一个元素是什么
for i in s:
    if i == pre_element: # 如果当前元素和上一个元素相同,连续出现次数+1,并更新最大值
        cur_repeat_time += 1 
        Max = max((cur_repeat_time, Max))
    else: # 不同则刷新计数器,配置pre  
        pre_element = i
        cur_repeat_time = 1

print(Max)

If the number of times - 1 appears, you have to judge whether it is equal to - 1. The initial value of the counter has to be 0, and else does not need to reset pre. Just reset the counter directly

s = [-175, -842, -187, -66, -1, -1, -148, -17, -32, -35, 35, -1, -1, -1, -278, 278, -432, -92, -25,
     549, -666, -45, -547, -38, -7, -101, -28, -43, -31, -4, 1510, -1958, -40, -25, -7, -1, -1, -1, -1, -15]
Max = 1 if s else 0  # 已知最大连续出现次数,列表为空则为0
cur_repeat_time = 0  # 记录当前元素是第几次连续出现
for i in s:
    if i == -1:  # 如果当前元素和上一个元素相同,连续出现次数+1,并更新最大值
        cur_repeat_time += 1
        Max = max((cur_repeat_time, Max))
    else:  # 不同则刷新计数器,配置pre
        cur_repeat_time = 0

print(Max)
Ove
commented on Wed, 11 May 2022

import itertoolss = [-175, -842, -187, -66, -1, -1, -148, -17, -32, -35, 35, -1, -1, -1, -278, 278, -432,-92, -25, 549, -666, -45, -547, -38, -7, -101,-28, -43, -31, -4, 1510, -1958, -40, -25, -7, -1, -1, -1, -1, -15]a=[len(list(v)) for k,v in itertools.groupby (s) If k = = - 1] print ('- 1 appears continuously for the maximum number of times: d% max (a))

virteract
commented on Wed, 11 May 2022
import itertools
s = [-175, -842, -187, -66, -1, -1, -148, -17, -32, -35, 35, -1, -1, -1, -278, 278, -432, -92, -25,
     549, -666, -45, -547, -38, -7, -101, -28, -43, -31, -4, 1510, -1958, -40, -25, -7, -1, -1, -1, -1, -15]
print(max([len(list(v)) for k,v in itertools.groupby(s)]))

four

iyomb
commented on Wed, 11 May 2022

Python practical techniques Chapter 11: find the most frequent elements in the sequence

Kai
commented on Wed, 11 May 2022
from collections import Counter

s = [-175, -842, -187, -66, -1, -1, -148, -17, -32, -35, 35, -1, -1, -1, -278, 278, -432, -92, -25,
     549, -666, -45, -547, -38, -7, -101, -28, -43, -31, -4, 1510, -1958, -40, -25, -7, -1, -1, -1, -1, -15]


c = Counter(s)
most = c.most_common(1)[0]
print(most[1])
lock This question has been locked and the reply function has been disabled.