def calculate_segments(thresholds, values):
alerts = []
for variable, threshold_info in thresholds.items():
alert_thresholds = threshold_info["alert_thresholds"]
variable_values = values[variable]
alert_type = None
alert_start = None
for i, value in enumerate(variable_values):
alert_type = len([threshold for threshold in alert_thresholds if value >= threshold])
if alert_type and alert_start is None:
alert_start = i
print(f'Alert type {alert_type} starts at index {i}')
alerts.append((alert_type, i))
return alerts
thresholds = {
'temperature': {
"alert_thresholds": [25.0, 30.0, 35.0],
},
'power_on': {
"alert_thresholds": [0, 1],
}
}
values = {
'temperature': [20.0, 28.0, 32.0, 40.0, 36.0, 25.0, 30.0, 37.0, 38.0, 39.0],
'power_on': [1, 1, 1, 1, 0, 0, 1, 1, 0, 0]
}
calculate_segments(thresholds, values)
我希望您创建一个生成警报的 Python 代码。 在这种情况下,温度警报将为:
不知道你到底需要什么,这里有一个创建警报列表的原型方法:
def temperature_alert(temp):
alert_level = sum(temp >= x for x in thresholds['temperature']['alert_thresholds'])
if alert_level:
return f'Type {alert_level}'
temperature_alerts = list(map(temperature_alert, values['temperature']))
# [None, 'Type 1', 'Type 2', 'Type 3', 'Type 3', 'Type 1', 'Type 2', 'Type 3', 'Type 3', 'Type 3']