【问题标题】:NoneType after List Error列表错误后的 NoneType
【发布时间】:2017-10-13 02:05:43
【问题描述】:

我无法理解第二个函数引发的错误。当我破解第二个函数时,我意识到 print(x) 给了我: [['安娜',74.0],['比尔',65.0],['Cal',98.0]] 无

由于这个无,我无法遍历列表并将其设置为 L。是什么导致了这个无?这是其他 3 个功能的粘贴箱。 code here!。非常感谢任何建议或朝着正确方向推动。


def string_avg(L):
'''(list of str) -> list of list
Given a list of strings where each string has the format:
'name, grade, grade, grade, ...' return a new list of 
lists where each inner list has the format :
[name (str), average grade (float)]
Requirement: This function should be 1 line long.

>>> string_avg(['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 
98'])
[['Anna', 74.0], ['Bill', 65.0], ['Cal', 98.0]]
'''
return(average_grade((string_list(L))))


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]
'''

x = string_avg(L)       

我对最后一个函数的预期代码是:

Say I changed the last function code to: 
x = string_avg(L)       

for i in range(0,len(x)):
    L[i] = x[i][1] 

但它说我找不到 len of nonetype,为什么我的 x 是 none 类型?

【问题讨论】:

  • 您是否运行过您在文档中编写的文档测试?结果如何?
  • x = string_avg(L) 设置了一个局部变量x,该变量在函数结束时丢失未使用。
  • @MichaelButscher 我得到了 ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 第二次返回给我函数(与输入相同)和所有其他函数的预期结果。我的目标是找出导致无的原因,然后设置 L[每个索引] = x[每个索引][1]。
  • 我将编辑我打算放在主帖中的代码。
  • 这是来自stackoverflow.com/q/46599506/4014959 的后续问题,我猜有几个人正在处理这个任务。 stackoverflow.com/q/46637076/4014959

标签: python list


【解决方案1】:

average_grade() 不返回列表,而只打印它并返回 None

【讨论】:

  • 这正是问题所在。它从我身边滑过。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 2021-09-02
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多