我想知道,当这个数是正数时,平均数是多少,当这个数是负数时,我想找出平均数。在
import csv
totalCount = 0
numberOfPositives = 0
with open('Weather30States.csv', 'r') as file1:
val = list(csv.reader(file1))[2]
val1 = val[0:4]
with open('FL%.csv', 'r') as file2:
reader = csv.reader(file2)
reader.next() # this skips the first row of the file
# this iteration will start from the second row of file2.csv
conditionMet = False
for row in reader:
if conditionMet == True:
if float(row[0].strip('%')) > 0: # change > to >= if you want to count 0 as positive
print "FA, 1",row[0],',', ','.join(row[1:5]) # print 1 if positive
numberOfPositives += 1 # add 1 to numberOfPositives only if positive
else:
print "FA, 0",row[0],',', ','.join(row[1:5]) # print 0 if not positive
totalCount += 1 # add 1 to totalCount regardless of sign
conditionMet = False # or break if you know you only need at most one line
if row[1:5] == val1:
conditionMet = True
print 'Total Count =', totalCount
print 'Percentage of Positive numbers =', numberOfPositives * 100./totalCount, '%'
我是Python和Excel的新手,我不知道在哪里或者如何去做。在
我想要这部分的第[0]行的平均值:
^{pr2}$
你可以收集你的正负值,然后在最后求平均值。在
从“%[0”的格式中可以看到它)。在
这段代码的工作原理是在列表中添加正值和负值。在循环的最后,取两个列表的平均值。根据您希望代码具有多大的弹性,您可能需要包含一个没有正值或没有负值的测试用例。在
相关问题 更多 >
编程相关推荐