在不匯入任何模塊的情況下查找串列中相同數字的最大連續出現。我有這個代碼
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() 函式中的問題
uj5u.com熱心網友回復:
嘗試使用這種方法:
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
uj5u.com熱心網友回復:
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([])
uj5u.com熱心網友回復:
發生的情況是,您僅在更改數字后(即,當 x != lst[i] 時)才檢查最大出現次數。由于 4 是串列中的最后一個值,因此從未測驗過它的計數。您必須在增加 num 的部分在 max1 和 num 之間移動 test。您還需要僅在 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
uj5u.com熱心網友回復:
最后我找到了解決方案,我們只需要在串列中添加假值,輸出就會很棒!在 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/370229.html
下一篇:檢查多個串列的最后一個元素
