在列表中找到相同数字的最大连续出现而不导入任何模块
Posted
技术标签:
【中文标题】在列表中找到相同数字的最大连续出现而不导入任何模块【英文标题】:find the largest consecutive occurrence of the same number in list without import any modules 【发布时间】:2022-01-07 03:18:22 【问题描述】:在不导入任何模块的情况下查找列表中相同数字的最大连续出现。我有这个代码
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() 函数中的问题
【问题讨论】:
未来提示:使用有意义的变量名。例如:current_count
比 newx
好得多。它可能看起来并不重要,但它可以让您推断代码的作用并更轻松地停止错误。
你能重新格式化代码吗,有些缩进看起来很奇怪?
【参考方案1】:
试试这个方法:
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
【讨论】:
谢谢,但错误还是一样【参考方案2】: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([])
【讨论】:
【参考方案3】:发生的情况是,您仅在更改数字后(即 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
【讨论】:
【参考方案4】:最后我找到了解决方案,我们只需要在列表中添加假值,输出就会很棒! 在 long() 函数下面是 edit*
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()
【讨论】:
以上是关于在列表中找到相同数字的最大连续出现而不导入任何模块的主要内容,如果未能解决你的问题,请参考以下文章