在[quant]字段中找到大于或等于(337)的值的平均值,这是定量字段
quant
100
7
109
204
28
292
105
254
441
401
410
14
15
51
96
403
75
31
109
17
Run Code Online (Sandbox Code Playgroud)
这是我试过的代码
import csv
total = count = 0
with open('3111111a.csv', newline='') as f:
reader = csv.reader(f)
next(reader, None)
for row in reader:
total += float(row[4])
count += 1
if count:
average = total / count
print('The average of the values is {}'.format(average))
Run Code Online (Sandbox Code Playgroud)
试试这个:
#!/bin/env python
import csv
from itertools import islice
total = count = 0
with open('3111111a.csv', newline='') as f:
reader = csv.reader(f)
# `isslice` will skip any header/title row.
# Generates a list of integers from the fourth CSV value
numbers = (int(row[4]) for row in islice(reader, 1, None))
# Generates another list of values that are >= 337
gt337 = [i for i in numbers if i >= 337]
# Sums all the numbers in our list then divides by the number to get the average
print (sum(gt337)/len(gt337))
Run Code Online (Sandbox Code Playgroud)
使用的最高分with!
您可以从文档中了解有关islice()和List Comprehensions的更多信息.
玩得开心Python:-)
| 归档时间: |
|
| 查看次数: |
211 次 |
| 最近记录: |