Altuser111
Altuser111

Reputation: 13

Fastest/ most efficient way to trigger an if statement if specific numbers are met

There are multiple ways to do this, but I'm looking for the fastest/ most efficient way.

I've got a counter, starting at 0, which increments every time an event triggers and goes up to 1000. If the counter gets to 101, 201, 301, 401, 501, 601, 701, 801 or 901, something needs to happen.

Most simple way:

        if counter == 101:
            Do something
        if counter == 201:
            Do something
        if counter == 301:
            Do something
        if counter == 401:
            Do something
        #etc .......

We could make it more efficient by the following:

    if counter < 501:
        if counter == 101:
            Do something
        if counter == 201:
            Do something
        #etc .......
    if counter >= 501:
        if counter == 501:
            Do something
        if counter == 601:
            Do something
        #etc .......

Or even putting it in a loop in order to add a break:

for i in range (1):
    if counter < 501:
        if counter == 101:
            Do something
            break
        if counter == 201:
            Do something
            break
        #etc .......
    if counter >= 501
        if counter == 501:
            Do something
            break
        if counter == 601:
            Do something
            break
        if counter == 701:
            Do something
        #etc .......

BUT, there problem is that it's always going to trigger when the counter is in between the numbers (1, 2, 3, 102, 105, 106, 450, 459, 891, etc).

Another way I was thinking of was as follows:

if (counter == 101) OR (counter == 201) OR (counter == 301): #etc
    Do something according to number

But that's basically the same as having a bunch of if statements like the first example.

Lastly, I was thinking about it only triggering if the counter = like x01 (so x can be any number followed by 01).

for i in (0):
if "01" in str(counter):
    if counter == 101:
        do something
        break
    if counter == 201: 
        do something
        break

Wondering what you guys think

*EDIT: some are asking what "do something" is. It's simply changing a variable. So if counter = 101 -> var = "hello", if counter = 201 -> var = "bye", etc..

Upvotes: 0

Views: 89

Answers (3)

gchapuis
gchapuis

Reputation: 163

Try using a dictionary containing all the values that need to trigger an action as key and a function as value.

Example:

mydict[101] = my_function

and then do:

if counter in mydict:
    mydict[counter](...)

Upvotes: 3

PurpleHacker
PurpleHacker

Reputation: 348

This is the most elegant way I could think of, not necessarily more efficient though.

for i in range(1000):
    if i % 100 == 1:
        # do something

Upvotes: 0

jkoestinger
jkoestinger

Reputation: 1010

With the initial question the response from gchapuis would have been good. Now, since you actually only want to set a variable value, you can keep the dictionary approach but remove the "callable" idea. With this in mind, you could do the following:

MAPPINGS = {
    101: "hello",
    201: "bye"
}

# later in your code

var = MAPPINGS.get(counter, var)  # give var as default, so it's unchanged if the counter value is not in the dict

Upvotes: 1

Related Questions