Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从字符串列表中返回非类型?_Python_Python 3.x - Fatal编程技术网

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

Python 如何从字符串列表中返回非类型?,python,python-3.x,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 float

这个函数有问题。当我试图运行它时,它不起作用。 谁能帮我修一下吗

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)
返回:

[74.0, 65.0, 98.0]
更新后:


循环遍历L并创建一个新列表split,它将L中有逗号的元素拆分。然后根据需要将字符串更改为浮动。然后对浮点数进行求和和和平均。将L中的元素设置为计算的平均值。

更新

现在我明白了你的意图(在@pm2ring的帮助下),这里有一个完全不同的答案(这个问题实际上比我原来想的要简单得多)。如果你对我最初的答案或我和其他人的各种评论做出回应,并更清楚地解释你想做的事情,那会很有帮助

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]

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

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

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()
在这方面效果更好。

那么到底是什么问题呢?我没有得到[74.0,65.0,98.0],这是我想从docstring中的示例中得到的。首先,你需要拆分每个字符串,以便从中提取数据。然后需要将名称后面的内容转换为floats。然后你可以在这些浮点数上做算术运算。然后您可以将结果保存回相应的列表项中。试着写一些代码来做这些事情。顺便说一句,你不想用
.isdigit()
来做这个。你知道这些字符串的格式。没有必要测试分数是否是数字,在本作业中,您可以假设分数是数字。@kbball:除了“它不起作用”之外,没有错误描述,问题标题甚至表明您不理解任务是什么。for z in range语句是否在新行中?当我运行它时,我得到了这个语句的名称,但是我只想要平均值,z的平均值可以这样写。。。代码对我有用。您的实际数据是否与L中显示的数据不匹配?我想要类似的东西>>>L=['Anna,50,92,80','Bill,60,70','Cal,98.5100,95.5,98']>>>string_avg_update(L)>>>L[74.0,65.0,98.0],但它对我不起作用。我的意思是,LIt后面的数字还表明,在尝试测试L时出现错误。zip是什么意思?比如它是干什么的?它在房间里。简而言之,它需要两个或多个列表,并生成(
yield
s)组(元组),这些组(元组)是从每个输入参数序列中的每个项的相同位置提取的元素。i、 e.
zip([1,2,3],[4,5,6])
返回一个iterable值,该值产生
(1,4)
(2,5)
,然后成功地产生
(3,6)
。如果您将它包装在
list()
构造函数调用中,您将得到一个包含
[(1,4)、(2,5)、(3,6)]
的列表。dg123:这是否回答了您关于
zip()
的问题?我的回答是否产生了正确的结果?如果没有,请编辑您的问题,并描述如何从输入值计算所需的值。为什么要查找列的平均值?您只需要每行的平均值。@PM2Ring:谢谢。通过阅读你的答案,我终于明白OP是如何得到这些值的。这个问题比我自己做的要简单得多。我觉得自己没有意识到这一点有点愚蠢,但他们可以在问题的前面加上一句话来澄清问题……列举了什么mean@dg123“
enumerate
…为我们提供每个列表项(
idx
)的索引和字符串本身(
student
)”。请参阅,以后,您应该尝试通过搜索文档来找到这些问题的答案。如果你不明白文档在说什么,那么在这里提问,引用你问题中的文档,这样人们就知道你不仅仅是在偷懒。是的,我下次一定会查出来,谢谢这个函数应该是修改输入列表,而不是创建一个新的。@user2357112,哦,对不起,我没有注意到:)相应地更改了代码。
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]