from pprint import pprint
temps = [18.0, 18.5, 18.7, 19.0, 18.9, 19.2, 19.5, 19.3, 19.2, 19.0, 19.1]
trends = []
for i, temp in enumerate(temps):
if i > 0:
if temp > prevTemp:
trend = 'UP'
else:
trend = 'DOWN'
if trend != prevTrend:
if trends:
trends[-1]['to'] = i - 1
trends[-1]['toTemp'] = prevTemp
trends.append({
'from': i - 1,
'fromTemp': prevTemp,
'trend': trend
})
if i == len(temps) - 1:
trends[-1]['to'] = i
trends[-1]['toTemp'] = temp
else:
trend = ''
prevTemp = temp
prevTrend = trend
print(i, '->', temp, trend)
print()
pprint(trends)
0 -> 18.0
1 -> 18.5 UP
2 -> 18.7 UP
3 -> 19.0 UP
4 -> 18.9 DOWN
5 -> 19.2 UP
6 -> 19.5 UP
7 -> 19.3 DOWN
8 -> 19.2 DOWN
9 -> 19.0 DOWN
10 -> 19.1 UP
[{'from': 0, 'fromTemp': 18.0, 'to': 3, 'toTemp': 19.0, 'trend': 'UP'},
{'from': 3, 'fromTemp': 19.0, 'to': 4, 'toTemp': 18.9, 'trend': 'DOWN'},
{'from': 4, 'fromTemp': 18.9, 'to': 6, 'toTemp': 19.5, 'trend': 'UP'},
{'from': 6, 'fromTemp': 19.5, 'to': 9, 'toTemp': 19.0, 'trend': 'DOWN'},
{'from': 9, 'fromTemp': 19.0, 'to': 10, 'toTemp': 19.1, 'trend': 'UP'}]