简体   繁体   中英

how do i reduce the amount of if statements in my code?

so my code has a lot of if statements:

            rnrp += 1
        if a100 == b100:
            rnrp += 1
        if a10 == b10:
            rnrp += 1
        if a1 == b1:
            rnrp += 1

and:

            if b1000 == a100:
                rnwp += 1
                break
            if b1000 == a10:
                rnwp += 1
                break
            if b1000 == a1:
                rnwp += 1
            break
        while true:
            if b100 == a1000:
                rnwp += 1
                break
            if b100 == a10:
                rnwp += 1
                break
            if b100 == a1:
                rnwp += 1
            break
        while true:
            if b10 == a1000:
                rnwp += 1
                break
            if b10 == a100:
                rnwp += 1
                break
            if b10 == a1:
                rnwp += 1
            break
        while true:
            if b1 == a1000:
                rnwp += 1
                break
            if b1 == a100:
                rnwp += 1
                break
            if b1 == a10:
                rnwp += 1
            break 

as you can see this is a lot of if statements, the first is fine, but the second needs improvements. Also what the second code is trying to accomplish is checking each place of b (100's place, 100's place, 10's place etc..) matches any of the places of a. How can I shrink the amount of if statement's in the second part of the code? this post has been answered by jasonharper (I cant find the button as of right now so I'm just putting it in the question) thanks!!

You can use collections like list or dict to reduce the number of operations. For instance,

    while true:
        if b100 == a1000:
            rnwp += 1
            break
        if b100 == a10:
            rnwp += 1
            break
        if b100 == a1:
            rnwp += 1
        break

Could be

        if b100 in [a1000, a10, a1]:
            rnwp += 1

Since you are repeating some of these patterns often, you could make some of these lists before hand and repeat their usage. It may even be useful to use dictionaries instead so that you don't have to remember the variables separately.

Use the elif statements. Other than that there's not too much more you can do there. Using many if statements can be confusing but as long as your code is clean then you can pull it off.

The way I would shortened it:

Boolean_value = True
while Boolean_value:
    if b100 == a1000: rnwp, Boolean_value = rnwp + 1, False 
    if b100 == a10: rnwp, Boolean_value = rnwp + 1, False 
    if b100 == a1: rnwp, Boolean_value = rnwp + 1, False 

It's pretty useful, if you want to an if statement and that if statement only have one expression. You can change multiple values in one line as well.

You could use product from itertools and do it all on a single line:

from itertools import product 

rnwp += sum(b==a for b,a in product([b1000,b100,b10,b1],[a1000,a100,a10,a1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM