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