[英]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.