目标是计算出长数中最常见的数字。例如,2135232455555将返回5。你知道吗
print('Question 4')
def most_frequent(number):
analysisnumber=map(int,str(number))
returnvalue=[0,0,0,0,0,0,0,0,0,0]
highest = 0
total2=0
for i in range(0,len(str(number))):
returnvalue[analysisnumber[i]] = list(map(i,analysisnumber))
for i in range(0,20):
if i < 10:
if returnvalue[i] > highest:
highest = returnvalue[i]
if i > 10:
if returnvalue[i-10] == highest:
total2+=highest
print("The most frequent number",end="")
if total2 > highest:
print("s are: ")
for i in range(0,10):
if returnvalue[i] == highest:
print(i)
else:
print(" is ", end="")
for i in range(0,10):
if returnvalue[i] == highest:
print(i)
break
number=int(input("Enter the number intended for analysis:"))
most_frequent(number)
我明白了
TypeError: 'int' object is not callable
错误。我需要帮助!你知道吗
您使用了错误的map函数。你知道吗
映射的第一个值(来自文档)是function):
map(function, iterable, ...)
你给它提供了一个int(i)。你知道吗
这样就可以了,使用Counter:
或者直接使用(感谢@tobias\k):
相关问题 更多 >
编程相关推荐