这是代码:
def mostFrequent(num1,n):
for i in range(1,n):
if (num1[i] == num1[i-1]):
curr_cnt += 1
else :
if (curr_cnt > max_cnt):
max_cnt = curr_cnt
res = num1[i-1]
curr_cnt = 1
if (curr_cnt > max_cnt):
max_cnt = curr_cnt
res = num1[n-1]
return res
num1 = data['num1'].values
num1.sort()
n = len(num1);max_cnt = 1;res = num1[0];curr_cnt = 1
print(mostFrequent(num1, n))
我在做什么错了?
答案 0 :(得分:0)
某些变量在mostFrequent
函数外部初始化。如果要在函数内部使用它们,它们必须是可用的。这是解决方法,
def mostFrequent(num1,n):
max_cnt = 1
res = num1[0]
curr_cnt = 1
for i in range(1,n):