def mode(given_list):
highest_list = []
highest = 0
index = 0
for x in range(0, len(given_list)):
occurrences = given_list.count(given_list[x])
if occurrences > highest:
highest = occurrences
highest_list[0] = given_list[x]
elif occurrences == highest:
highest_list.append(given_list[x])
该代码旨在计算给定列表的模式。我不明白我出错的地方。IndexError:列表分配索引超出范围Python
准确的错误我正在收到。
line 30, in mode
highest_list[0] = given_list[x]
IndexError: list assignment index out of range
您可以使用'g.get'而不是'lambda x:g [x]'。 – TigerhawkT3