[Python] best way to map a value from range number in python?

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    Say we have a data example like this:

    AQI LEVEL
    0~50 1
    51~100 2
    101~150 3
    151~200 4
    201~300 5
    >300 6

    What's the best way to return the Level of AQI by given a AQI value(ex:100, return 2)

    def get_level_of_aqi(aqi):
    # do sth map idea like here
    return level_of_aqi


    My current method:

    def get_level_of_aqi(aqi):
    if 0 <= aqi <= 50:
    level = 1
    elif 51 <= aqi <= 100:
    level = 2
    ....

    return level


    Is there any better way to improve ?

    Continue reading...

Compartilhe esta Página