如何从字符串列表中返回非类型?

时间:2017-10-08 23:33:02

标签: python python-3.x

我遇到了这个功能的问题。当我尝试运行它时,它不起作用。 有人可以帮我解决一下吗?

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)

4 个答案:

答案 0 :(得分: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并创建一个新列表split,它将L中的元素拆分为逗号。然后在需要的地方将字符串更改为浮点数。然后在浮点上运行求和和平均值。将L中的元素设置为计算的平均值。

答案 1 :(得分: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]

答案 2 :(得分:0)

我们使用enumerate循环遍历列表。这为我们提供了每个列表项(idx)和字符串本身(student)的索引。

然后我们在每个字符串上调用split来提取其内容,将第一个项目保存为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]

答案 3 :(得分: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()对此更有效。