如果满足两个条件则打印出来

时间:2016-07-19 17:00:22

标签: python

我想要做的是修改下面的dT的if语句。现在我把它打印出来"温度" " DT" "稳态"如果dT在-.5和.5之间,但我希望它只在dT值在-.5和.5之间连续十次迭代时打印出这三个项目。如果没有,它应该打印出来"温度" " DT"正如其他声明所暗示的那样。

temperature = []
dT_tol = .5

# Read loop
for i in range(60):
    # Get the thermocouple reading on AIN0. 
    tempC = ljm.eReadName(handle, "AIN0_EF_READ_A")
    temperature.append(tempC)
    dT = temperature[i]-temperature[i-1]

    if -dT_tol<dT<dT_tol:
        print "Temperature:","%.3f"% temperature[i],"         " "dT:", "%.3f"% dT, "         " "Steady State" 
        sleep(1)
    else:
        print "Temperature:","%.3f"% temperature[i],"         " "dT:", "%.3f"% dT
        sleep(1)

3 个答案:

答案 0 :(得分:1)

只需使用if代替for。同时使用0.5代替.5以提高可读性。考虑使用format方法显示字符串,特别是如果您使用的是Python 3x。

答案 1 :(得分:1)

我认为这是你可能想要的:

我正在用新代码编辑我的答案。如果这是错误的事情,我将删除此答案并添加一个新答案。我的思绪很接近,但有点混乱。

此代码首先查找数据超出范围的所有位置并记录其索引(停止)。然后检查从“停止”到“停止”以查看范围是否大于2并在最终结果中收集这些值。

temp_list = []

# first make up some data.  These are all positive, between 0 and .7.  In your case you would use your temperature data
temp_list = [.6, .5, .4, .3, .5, .6, .4, .3, .2, .8, .4]
print "temp_list"
print temp_list

# we want to print data where at least 3 in a row are within -.5 and .5

print "---------"


results = []
stops = [(i, d) for i, d in enumerate(temp_list) if abs(d)>.5]
stops.append([len(temp_list), 10])
last_stop = 0
print stops

for pos in stops:
   if pos[0]-last_stop>2:
       results.extend(temp_list[last_stop+1:pos[0]])
       last_stop = pos[0]
       print results

print "done"

结果:

temp_list
[0.6, 0.5, 0.4, 0.3, 0.5, 0.6, 0.4, 0.3, 0.2, 0.8, 0.4]
---------
[(0, 0.6), (5, 0.6), (9, 0.8), [11, 10]]
[0.5, 0.4, 0.3, 0.5]
[0.5, 0.4, 0.3, 0.5, 0.4, 0.3, 0.2]
done

答案 2 :(得分:0)

你可以使用一个count变量,只要条件连续被击中10次或条纹被打破就将count变量设置为0