试图找到多行中元素[0]的平均值

时间:2014-11-28 22:15:56

标签: python

您好我正在尝试将一大堆列表中的所有第一个元素加在一起,例如我有 [20,Private,266015,Some-college,10,Never-married,Sales,Own-child,Black,Male,0,0,44,United States,< = 50K](年龄是这里的第一个元素案例20)我需要找到一个人获得< = 50K的人的平均年龄,有超过30,000个记录,如此,我怎样才能在下一个记录中添加每个年龄?这是我的代码我得到的东西,但它是不对的。任何帮助都会非常感谢。

    for row in f:
        row = row.split(", ")
        try:
            row[0] = int(row[0])
            row[2] = None
            row[3] = None
            row[4] = int(row[4])
            row[10] = int(row[10])
            row[11] = int(row[11])
            row[12] = int(row[12])
            row[13] = None

            if "?" in row:
                continue
            if row[-1] == ">50K":
                total_count +=1
                age_over_count = row[0]+row[0]
                average_age_over = age_over_count/total_count

1 个答案:

答案 0 :(得分:1)

如果你想找到给定row[0] row[-1]== ">50k"的平均值,你需要在循环结束后计算平均值,然后继续用你已经拥有的值添加你的age_over_count,即+ = ...

for row in f:
    row = row.split(", ")
    try:
        row[0] = int(row[0])
        row[2] = None
        row[3] = None
        row[4] = int(row[4])
        row[10] = int(row[10])
        row[11] = int(row[11])
        row[12] = int(row[12])
        row[13] = None

        if "?" in row:
            continue
        if row[-1] == ">50K":
            total_count +=1
            age_over_count += row[0]          # <- Keeps adding what you have.
average_age_over = age_over_count/total_count # <- Calculates average.