我在使用此功能時遇到問題。當我嘗試運行它時,它不起作用。 任何人都可以幫我修好嗎?如何從字符串列表中返回一個非類型?
def string_avg_update(L):
'''(list of str) -> NoneType
Given a list of strings where each string has the format:
'name, grade, grade, grade, ...' update the given
list of strs to be a list of floats where each item
is the average of the corresponding numbers in the
string. Note this function does NOT RETURN the list.
>>> L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
>>> string_avg_update(L)
>>> L
[74.0, 65.0, 98.0]
'''
average = 0
for item in L:
if item.isdigit():
average = sum(item)/len(item)
那究竟是什麼問題? – Mureinik
我沒有得到[74.0,65.0,98.0],這是我想從文檔字符串 – dg123
中的示例中得到的東西首先,您需要拆分每個字符串,以便從中提取數據。然後你需要把名字後的東西轉換成浮動。然後你可以對這些浮點數進行算術運算。然後您可以將結果保存回相應的列表項。試着寫一些代碼來完成這些事情。 –