在不导入任何模块的情况下,在列表中查找相同数字的最大连续出现次数



在不导入任何模块的情况下,在列表中查找相同数字的最大连续出现次数。我有这个代码

def reads():
lst=[] #create empty list
flag=True #create flag
N1=input("Input a number to add to list or 0 to stop: ") #read value
while flag: #check if invalid or not

if not N1.isdigit():
print("Invalid input")
N1=input("Input a number to add to list or 0 to stop: ") #re read if invalid
elif N1=="0": #stop if 0
flag=False
else:
lst.append(N1) #add to empty list if valid
N1=input("Input a number to add to list or 0 to stop: ") # re read
lst=list(map(int, lst)) #convert to integer
return lst #return
def long(lst):
newx=0 #count
x=lst[0] 
max1=0 #to save the how many number dupilicted
num=lst[0] #to save which number is
for i in range(1,len(lst)):
if x==lst[i]: 

newx=newx+1
else:
newx=newx+1
if max1<newx:
max1=newx
num=x
x=lst[i]
newx=0
else:
newx=0
x=lst[i]
return max1,num
def main(): # to call other functions and display the results
x=reads() 
m,a=long(x)
print("List: ",x)
print("The number is: ",a)
print("The largest size of consecutive numbers: ", m)
main()

程序运行得很好,但有错误如果我输入1 1 2 3 4 4 4 0名单将是,

lst=[1,1,2,3,4,4,4]

并且输出必须是

The number is: 4
The largest size of consecutive numbers: 3

但它是这样的:

The number is: 1
The largest size of consecutive numbers: 2

long((函数中的问题

尝试这种方法:

def long(lst):
max_count = 1
max_num = lst[0]
count = 1
for prec, num in zip(lst[:-1], lst[1:]):
if num != prec:
if count > max_count:
max_count = count:
max_num = prec
count = 1
else:
count += 1
return max_num, max_count

cont_cnt将继续对输入列表中的数字进行计数,如果数字与前一个不同,则重置计数器。然后max_num选择最大值。如果列表为空,则返回default=(None, None)

def cont_cnt(lst):
prev = object() # guaranteed not to be present in the list
for x in lst:
if x != prev:
count = 0 
prev = x 
count += 1
yield (count, x)
def max_num(lst):
count, number = max(cont_cnt(lst), default=(None, None))
print(f"Max: {number=}, {count=}")
max_num([1,1,2,3,4,4,4])
max_num([])

发生的情况是,只有在更改数字后(即当x!=lst[i]时(才检查最大出现次数。由于4是列表中的最后一个值,因此它的计数从未经过测试。在增加num的部分,你必须在max1和num之间移动测试。你还需要只在x等于lst[i]时增加num,这意味着值的计数应该用1:启动

def long(lst):
newx=1 #count = 1
x=lst[0] 
max1=newx #to save the how many number     dupilicted
num=x #to save which number is
for i in range(1,len(lst)):
if x==lst[i]: 
# compute new value if consecutive counts
newx=newx+1
# check new value of newx 
if max1<newx:
max1=newx
num=x
else:
# different values : start new counting
newx=1
x=lst[i]
return max1,num


最后我找到了解决方案,我们只需要在列表中添加假值,输出就会很棒!在long((函数下面编辑*

def reads():
lst=[] #create empty list
flag=True #create flag
N1=input("Input a number to add to list or 0 to stop: ") #read value
while flag: #check if invalid or not
if not N1.isdigit():
print("Invalid input")
N1=input("Input a number to add to list or 0 to stop: ") #re read if invalid
elif N1=="0": #stop if 0
flag=False
else:
lst.append(N1) #add to empty list if valid
N1=input("Input a number to add to list or 0 to stop: ") # re read
lst=list(map(int, lst)) #convert to integer
return lst #return
def long(lst):
lst.append(0) #add fake value to correct the function
newx=0 #count
x=lst[0] 
max1=0 #to save the how many number dupilicted
num=lst[0] #to save which number is
for i in range(1,len(lst)):
if x==lst[i]: 

newx=newx+1
else:
newx=newx+1
if max1<newx:
max1=newx
num=x
x=lst[i]
newx=0
else:
newx=0
x=lst[i]
return max1,num
def main(): # to call other functions and display the results
x=reads() 
print("List:" ,x) #print the list without adding 0 , the fake value****
m,a=long(x)

print("The number is: ",a)
print("The largest size of consecutive numbers: ", m)
main()

最新更新