如何调试此错误“ float”对象不可迭代?

时间:2019-04-19 06:57:09

标签: python

我正在尝试计算列表列表中数字的平均值,我应该跳过第一个观察值(即位置0),因为它们是国家/地区的名称,我只需要计算[0]之后的每一列的平均值。 但是,我的代码一直在说:

  

TypeError:“ float”对象不可迭代。

如果有人可以看一下并提供帮助,我将不胜感激。谢谢!

p.s我不能使用numpypandas

test_list = [['name1', 6.039330006, 0, 0.00, 67.53870392, 0.831966162, -0.186299905, None], ['name2', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.00001]]  
def my_mean(file): 

# This function calculates the mean value of each column in the file

    mean_list = [ ]
    for row in range(0,len(file)): 
        for column in range(1,len(file[0])):
            if file[row][column] != None:
               mean_value = stat.mean(file[row][column])
               # mean_value = sum(file[row][column])/ len(1,file[row][column])
        mean_list.append(mean_value)
    print("The mean values are {0}".format(mean_list))

使用类似的代码,我可以得到:

[-0.186299905, 0.246900991]中两列的最小值为test_list

3 个答案:

答案 0 :(得分:1)

对于您数据的每一列,我们构建一个包含其值的列表,过滤掉None个值,并将其传递给mean

import statistics

def my_mean(file): 
    # This function calculates the mean value of each column in the file
    mean_list = [ ]
    for column in range(1,len(file[0])):
        column_values = (row[column] for row in file)
        mean_list.append(statistics.mean(val for val in column_values if val is not None))

    return mean_list          


test_list = [['name1', 6.039330006, 0, 0.00, 67.53870392, 0.831966162, -0.186299905, None], ['name2', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.00001]]  
print(my_mean(test_list))

# [5.163533211000001, 4.517355442, 0.348962456, 66.33219528500001, 0.722831607, -0.159233041, 1e-05]

您还可以使用zip来构建列列表,然后计算列表推导中的均值:

import statistics

def my_mean(file): 
    columns = list(zip(*file))[1:]
    mean_list = [statistics.mean(val for val in column if val is not None) for column in columns]
    return mean_list          


test_list = [['name1', 6.039330006, 0, 0.00, 67.53870392, 0.831966162, -0.186299905, None], ['name2', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.00001]]  
print(my_mean(test_list))

# [5.163533211000001, 4.517355442, 0.348962456, 66.33219528500001, 0.722831607, -0.159233041, 1e-05]

答案 1 :(得分:1)

如果我遵循您的方法,我会这样做:

def my_mean(file): 
mean_list = []
for row in range(len(file)): 
    temp_column = []
    for column in range(len(file[row])):
        if isinstance(file[row][column], (int, float)):
            temp_column.append(file[row][column])

    mean_value = sum(temp_column) / len(temp_column)
    mean_list.append(mean_value)

print("The mean values are {0}".format(mean_list))

答案 2 :(得分:0)

怎么样:

test_list = [['name1', 6.039330006, 0, 0.00, 67.53870392, 0.831966162, -0.186299905, None],
             ['name2', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.00001]]  
def mean(x):
    i=r=0.0
    for c in x:
        if c or c==0: 
            r+=c;i+=1
    return r/i

numbers_list = [row[1:] for row in test_list]

print('Result')
print([mean(x) for x in zip(*numbers_list)])

它给出:

结果

[5.163533211000001,9.034710884,0.697924912,66.33219528500001,0.722831607,-0.159233041,1e-05]