2017-10-08 83 views
-3

我在使用此功能時遇到問題。當我嘗試運行它時,它不起作用。 任何人都可以幫我修好嗎?如何從字符串列表中返回一個非類型?

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) 
+3

那究竟是什麼問題? – Mureinik

+0

我沒有得到[74.0,65.0,98.0],這是我想從文檔字符串 – dg123

+1

中的示例中得到的東西首先,您需要拆分每個字符串,以便從中提取數據。然後你需要把名字後的東西轉換成浮動。然後你可以對這些浮點數進行算術運算。然後您可以將結果保存回相應的列表項。試着寫一些代碼來完成這些事情。 –

回答

0
def string_avg_update(L): 
    for x in range(len(L)): 
     split = L[x].split(',') 
     for y in range(1, len(split)): 
      split[y] = float(split[y]) 
     summation = sum(split[z] for z in range(1, len(split))) 
     average = summation/(len(split)-1) 
     L[x] = average 

L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 

string_avg_update(L) 

print(L) 

返回:

[74.0, 65.0, 98.0] 

更新後:

遍歷L和創建新的列表,拆分,其將在L,其中有逗號的元素。然後在需要的地方將字符串更改爲浮動。然後運行花車上的總和和平均值。將L中的元素設置爲計算的平均值。

+0

是for for z新範圍內的範圍說明? – dg123

+0

當我運行它的時候,我得到了這個名字,但我只想要平均值 – dg123

+0

沒有for z可以像這樣寫...代碼適用於我。您的實際數據與您在L中顯示的數據不符? – kbball

0

更新

現在我明白你正在試圖做的(與@PM 2Ring的幫助下)什麼,這裏是一個完全不同的答案(這個問題實際上比我原以爲簡單)。這將有有益的,如果你回答要麼我原來的答覆或從自己和他人各種意見,並解釋你試圖更清晰地做......

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 row 
    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] 
    ''' 
    # Replace contents of L with average of numeric values in each string elem. 
    for i, record in enumerate(L): 
     grades = [float(grade) for grade in record.split(',')[1:]] 
     L[i] = sum(grades)/len(grades) 

    # Function will implicitly return None since there's no return statement. 

if __name__ == '__main__': 

    L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 
    print(L) 
    string_avg_update(L) 
    print(L) # -> [74.0, 65.0, 98.0] 
+0

zip是什麼意思?像它做什麼? – dg123

+0

[在線文檔](https://docs.python.org/3/index.html)中的[right here](https://docs.python.org/3/library/functions.html#zip) 。簡而言之,它需要兩個或兩個以上的列表,並生成從每個輸入參數序列中每個項目中相同位置提取的元素(「元組」)。即'zip([1,2,3],[4,5,6])'返回一個迭代產量'(1,4)','(2,5)',然後'(3,6)'' sucessively。如果你把它包裝在一個'list()'構造函數調用中,你最終會得到一個包含'[(1,4),(2,5),(3,6)]''的列表。 – martineau

+0

dg123:這是否回答你關於'zip()'的問題?我的答案是否產生了正確的結果?如果不是,請編輯您的問題並描述如何根據輸入值計算所需的值。 – martineau

0

我們遍歷使用enumerate名單。這給了我們每個列表項目(idx)和字符串本身(student)的索引。

然後我們調用拆分每個字符串來提取其內容,保存第一項爲name,其餘爲data。然後我們將data中的字符串轉換爲浮點數。然後我們計算這些浮點數的平均值。然後我們將平均值保存回適當的列表項。

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] 
    ''' 
    for idx, student in enumerate(L): 
     name, *data = student.split(',') 
     data = [float(u) for u in data] 
     L[idx] = sum(data)/len(data) 

L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 
print(L) 

string_avg_update(L) 
print(L) 

輸出

['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 
[74.0, 65.0, 98.0] 
+0

枚舉意味着什麼 – dg123

+0

@ dg123「'enumerate」...給我們的索引每個列表項('idx')和字符串本身('student')「。請參閱https://docs.python.org/3/library/functions.html#enumerate未來,您應該通過搜索文檔來嘗試找到這些問題的答案。如果你不明白文件是什麼意思,那麼請問這裏,引用你的問題中的文檔,以便人們知道你不只是懶惰。 –

+0

雅,我一定會在下次檢查,謝謝 – dg123

0

試試這個。

def string_avg_update(lst): 

    for i in range(len(lst[:])): 
     grades = lst[i].split(',')[1:] 
     float_grades = [float(grade) for grade in grades]   
     average = sum(float_grades)/len(float_grades)   
     lst[i] = average 



L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 
string_avg_update(L)  

>>> print(L) 
[74.0, 65.0, 98.0] 

我知道enumerate()對此更好。

+1

該函數應該改變輸入列表,而不是一個新的。 – user2357112

+0

@ user2357112,哦,對不起,我沒有注意到:)相應地更改了代碼。 – srig

相關問題