嵌套列表進行遍歷如何將列表的索引與浮點數進行比較?
cost =[[125900], [115000],
[105900],
[85000],
[150000],
[155249],
[97500]]
初始化變量
index = 0
cost_len = len(cost)
below_avg = 0
above_avg = 0
total = 0
for循環來計算成本
for i in cost:
total = total + sum(i)
print(total)
成本計算的平均總所有元素的
avg = total/len(cost)
嘗試計算索引是否高於或低於平均
for i in cost:
while index <= cost_len:
if i > avg:
above_avg+=1
elif i < avg:
below_avg +=1
index+=1
當試圖評價成本的索引,則返回「類型錯誤:unorderable類型:列表()>浮動()」。我如何比較列表的指數和變量avg?
在上一個代碼位中,什麼是'index','cost_len','above_avg','below_avg'?它們從未被顯示爲被初始化。 –
不用擔心。我收回了我的近距離投票。 –