2017-10-08 53 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

相关问题