在数组中创建段

问题描述 投票:0回答:1
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 代码。 在这种情况下,温度警报将为:

  • 类型 1(25 到 30 之间)= 从索引 1 到索引 1
  • 类型 2(30 到 35 之间)= 从索引 2 到索引 2
  • 类型 3(超过 35)= 从索引 3 到索引 4
  • 类型 1(25 到 30 之间)= 从索引 5 到索引 6
  • 类型 3(超过 35)= 从索引 7 到索引 9
python arrays segment temporal
1个回答
0
投票

不知道你到底需要什么,这里有一个创建警报列表的原型方法:

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']
© www.soinside.com 2019 - 2024. All rights reserved.