在Python上查找平均值

在Python上查找平均值,python,average,Python,Average,[Num]字段中(3987)和(5026)范围内的数字的平均值是多少 该字段位于下面,它是excel上的字段[4] **amount code quan val number Random** 2.11 I[N8U7]:75 184 Blue 2254 Potato 3.13 Z[V0L8]:64 131 Blue 6349 Carrot 4.24 B[Y1U2]:38 56 Blue 4164 Mushr

[Num]字段中(3987)和(5026)范围内的数字的平均值是多少

该字段位于下面,它是excel上的字段[4]

**amount       code    quan val     number  Random**
2.11    I[N8U7]:75  184 Blue    2254    Potato
3.13    Z[V0L8]:64  131 Blue    6349    Carrot
4.24    B[Y1U2]:38  56  Blue    4164    Mushrooms
7.32    T[Z7N0]:67  329 Red     2079    Pear
9.1     C[T8C5]:83  344 Blue    1045    Apple
11.17   M[P4J9]:38  267 Blue    1254    Strawberry
2.21    E[S1G7]:62  446 Red     2223    Vanilla
1.41    W[M4M5]:96  8   Red     6745    Juice
2.31    W[P3E1]:24  215 Red     1223    Orange
0.12    E[M5K0]:78  424 Blue    2385    Pineapple
3.91    A[A9M2]:33  367 Red     3354    Grape
3.1     W[N2E2]:70  121 Blue    7716    Watermelon
10.21   J[H2W8]:17  253 Red     1017    Yogurt
5.1     G[K5L5]:08  216 Red     1039    Peppers
1.14    V[Z2C3]:L75 419 Blue    2520    Onions
1.02    Q[I1I2]:20  380 Red     2700    Chocolate
0.19    S[P1X2]:43  133 Blue    3171    Cheese
7.21    Z[B2E3]:46  126 Blue    2971    Ham
10.21   L[F6V1J:28  249 Red     7574    Blueberry
1.02    X[B0N3]:65  243 Blue    3441    Water
我已经尝试了以下代码,我不知道还能做什么。提前谢谢

file=open ('3114644b.csv','r')

def mylist():
    alist=[]
    for line in file:
        field = line.split(',')
        if field[0]=='bid' or field[0]=='leave':
            alist.append(float(field[4]))
    return alist

blist=mylist()
total = 0
count = 0
for num in blist:
    total +=  num
    count += 1

average = total / count

print ("the average of the values)

file.close
使用,它是为以下任务设计的:

import csv

total = count = 0

with open('3114644b.csv', newline='') as f:
    reader = csv.reader(f)
    next(reader, None)  # skip the first row of headers

    for row in reader:
        total += float(row[4])
        count += 1

if count:
    # only calculate the average if there was actually anything in the file
    average = total / count
    print('The average of the values is {}'.format(average))
上述代码还将该文件用作上下文管理器(使用
with
语句);一旦
with
块完成,文件将自动为您关闭

在您的版本中,您试图手动关闭该文件,但仅设法引用
.close()
方法,而不是实际调用它

我们也不会把所有的值都读入一个列表,当你可以只求和这些值,并记下你求和的值的数量时就不会了。这使用了更少的内存,使得处理大型CSV文件的速度更快、效率更高

对于示例数据,上述代码输出:

平均值为3261.2

使用,它是为以下任务设计的:

import csv

total = count = 0

with open('3114644b.csv', newline='') as f:
    reader = csv.reader(f)
    next(reader, None)  # skip the first row of headers

    for row in reader:
        total += float(row[4])
        count += 1

if count:
    # only calculate the average if there was actually anything in the file
    average = total / count
    print('The average of the values is {}'.format(average))
上述代码还将该文件用作上下文管理器(使用
with
语句);一旦
with
块完成,文件将自动为您关闭

在您的版本中,您试图手动关闭该文件,但仅设法引用
.close()
方法,而不是实际调用它

我们也不会把所有的值都读入一个列表,当你可以只求和这些值,并记下你求和的值的数量时就不会了。这使用了更少的内存,使得处理大型CSV文件的速度更快、效率更高

对于示例数据,上述代码输出:

平均值为3261.2


这是一个不使用
cvs
模块的版本。(请注意,鼓励您学习如何使用适用于每项任务的模块)


这是一个不使用
cvs
模块的版本。(请注意,鼓励您学习如何使用适用于每项任务的模块)


我修正了你的缩进,但是你需要自己添加
.close()
调用括号。谢谢你Martin Pieters要找到平均值,你可以只做
avg=sum(blist)/len(blist)
@karthikr你能帮我修改一下代码吗?我是Python的初学者,所以我不太懂。这是Python 2还是Python 3?我修正了你的缩进,但你需要添加
.close()
自己调用括号。谢谢Martin Pieters找到平均值,你只需做
avg=sum(blist)/len即可(布利斯特)
@karthikr你能帮我修改一下代码吗?我是Python的初学者,所以我不完全理解。这是Python 2还是Python 3?应该是
if count
,而不是
if total
,因为平均值很可能是
0
@ThijsvanDien:事实上,我想避免
零分错误
,然后测试g表示
count
更有意义。虽然输入值都是正整数,但在特定范围内。@ThijsvanDien:也许,在另一位学员想要避免
尝试
之后,我在这里感觉到了一种询问情绪。
除了
语句之外..应该是
if count
而不是
if total
,因为e很可能是
0
@ThijsvanDien:事实上,我想避免出现
ZeroDivisionError
,然后测试
count
更有意义。虽然输入值在特定范围内,但都是正整数。@ThijsvanDien:也许,在另一个学习者想要避免
尝试之后,我在这里有一种询问的心情
除了
语句。。