我是 python 新手,正在编写一个程序来计算行数。该文件如下所示:
0.86149806
1.8628227
-0.1380086
-1
0.99927421
-1.0007207
0.99927421
0.99926955
-1.0007258
我的代码尝试如下:
counterPos = 0
counterNeg = 0
counterTot = 0
counterNeu = 0
with open('test.txt', 'r') as infile:
for line in infile:
counterTot += 1
for i in line:
if i > 0.3:
counterPos += 1
elif i < -0.3:
counterNeg += 1
else:
counterNeu += 1
我试图让它计算所有低于 -0.3 到counterNeg
的行,所有高于 0.3 的行counterPos
,以及所有数字在 0.29 到 -0.29 到 之间的行counterNeu
。
虽然它似乎不起作用,我知道我错了,for i in line
但不知道怎么做。