我在使用此功能时遇到问题。当我尝试运行它时,它不起作用。 任何人都可以帮我修好吗?如何从字符串列表中返回一个非类型?
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
中的示例中得到的东西首先,您需要拆分每个字符串,以便从中提取数据。然后你需要把名字后的东西转换成浮动。然后你可以对这些浮点数进行算术运算。然后您可以将结果保存回相应的列表项。试着写一些代码来完成这些事情。 –