简体   繁体   English

如何调试此错误? 'generator'类型的对象没有len()

[英]How can I debug this error? object of type 'generator' has no len()

I'm trying to compute the mean values of the numbers in a list of lists, I'm supposed to skip over the first observation (ie position 0) because they are the names of countries, I only need to calculate the mean of every column after [0] . 我正在尝试计算列表列表中数字的平均值,我应该跳过第一个观察值(即位置0),因为它们是国家/地区的名称,我只需要计算每个[0]之后的列。 However, my code keeps saying: 但是,我的代码一直在说:

TypeError: object of type 'generator' has no len()

If anyone could have a look and offer some help, I would greatly appreciate it. 如果有人可以看一下并提供帮助,我将不胜感激。 Thank you! 谢谢!

I've tried this code: 我已经试过这段代码:

def my_mean1(file):    
    mean_list = []
    for column in range(1,len(file[0])):
        column_values = (row[column] for row in file)

        # calculate the mean but not with the statistics module
        mean_values = sum(column_values) / len(column_values)
        mean_list.append(mean_values(x for x in column_values if x is not None))    
    return mean_list

The results I expect 我期望的结果

test = [['str1', 2.66171813, 7.460143566, 0.490880072, 52.33952713, 0.427010864, -0.106340349, 0.261178523], ['str2', 4.639548302, 9.373718262, 0.637698293, 69.05165863, 0.74961102, -0.035140377, 0.457737535]]
test1 = data(file) # a function that made the list of lists
my_mean(test)
[5.022380471333333, 9.327360550833333, 0.7483190200000001, 65.42301750166666, 0.6615842928333333, -0.05882061216666667, 0.3449308604]

but the error I encounter when I use test 但是我使用test时遇到的错误

TypeError: object of type 'generator' has no len()

and the error I encounter when I try test1 以及尝试test1时遇到的错误

TypeError: unsupported operand type(s) for +: 'int' and 'str'
column_values = (row[column] for row in file)

Replace the parenthesis with square brackets so you get a list returned instead of the generator type. 用方括号替换括号,以便获得返回的列表,而不是生成器类型。 []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'generator' 类型的对象没有 len() - object of type 'generator' has no len() 指标约束('generator'类型的对象没有len()) - Indicators constraints (object of type 'generator' has no len()) TypeError: 'generator' 类型的 object 没有 len() - TypeError: object of type 'generator' has no len() 如何解决错误:for i in range(len(val)): TypeError: object of type 'NoneType' has no len() ,这可能很傻 - how to resolve Error : for i in range(len(val)): TypeError: object of type 'NoneType' has no len() , it might be silly 如何在 Python 中调试“TypeError:'StringVar' 类型的对象没有 len()”? - How to debug “TypeError: object of type 'StringVar' has no len()” in Python? 如何修复“类型为'CategoricalDtype'的对象没有len()”类型的错误? - How to fix error of type “object of type 'CategoricalDtype' has no len()”? 类型错误:“生成器”类型的对象没有 len() Python random.shuffle(param) 错误 - TypeError: object of type 'generator' has no len() Python random.shuffle(param) error TypeError:“int”类型的 object 在 python(密码生成器)中没有 len() - TypeError: object of type 'int' has no len() in python (Password Generator) 如何处理这个错误? 类型错误:'float' 类型的 object 没有 len() - How to deal this error? TypeError: object of type 'float' has no len() TypeError:“函数”类型的 object 没有 len() - 如何修复此错误? - TypeError: object of type 'function' has no len() - How do i fix this error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM