David David - 7 months ago 13
Python Question

Alternative to large "if-else" tree algorithm?

brand new here, and fairly new to programming. I have written a program in Python that summarizes or "compresses" large UV-Vis absorbance data sets commonly encountered in chemistry and physics. I am trying to add the final touches to the program by making sections more "Pythonic". The following snippet is one such example.

while j < (N):
j=j+1
B=A[1+(10*j)+R][1:k] #Extraction expression.
C=[float(i) for i in B] #Float conversion.
D[0].append(C[0]),D[1].append(C[1]),D[2].append(C[2])
if NC >= 4:
D[3].append(C[3])
if NC >= 5:
D[4].append(C[4])
if NC >= 6:
D[5].append(C[5])
if NC >= 7:
D[6].append(C[6])
if NC >= 8:
D[7].append(C[7])
if NC >= 9:
D[8].append(C[8])
if NC >= 10:
D[9].append(C[9])
if NC >= 11:
D[10].append(C[10])
if NC == 12:
D[11].append(C[11])


C is a nested list. I am trying to append the index-specific elements contained within the lists inside C.

I.e. C = [[A1, B1, C1],[A2, B2, C2],[A3, B3, C3],...]
-- > [A1, A2, A3,...An], [B1, B2, B3,...Bn],...

The problem is that if NC is equivalent to some index N, the indices of the nested lists are of the same index, and the append algorithm is different for each value of NC.

The above snippet works perfectly for my purposes, however as the program evolves, the length and duplicate code is not feasible long term.

Can someone point me in the right direction?

Cheers

Answer

Note first of all that it's not necessary to nest them; if NC >= x, you know it's also going to be >= x - 1. That means you can "flatten" your tree to look like this:

while j < (N):
    j=j+1
    B=A[1+(10*j)+R][1:k] #Extraction expression.
    C=[float(i) for i in B] #Float conversion.
    D[0].append(C[0]),D[1].append(C[1]),D[2].append(C[2])
    if NC >= 4:
        D[3].append(C[3])
    if NC >= 5:
        D[4].append(C[4])
    if NC >= 6:
        D[5].append(C[5])
    if NC >= 7:
        D[6].append(C[6])
    if NC >= 8:
        D[7].append(C[7])
    if NC >= 9:
        D[8].append(C[8])
    if NC >= 10:
        D[9].append(C[9])
    if NC >= 11:
        D[10].append(C[10])
    if NC == 12:
        D[11].append(C[11])

Now in any situation where you're just doing the same thing over and over again with slightly different values, you can use a loop:

while j < (N):
    j=j+1
    B=A[1+(10*j)+R][1:k] #Extraction expression.
    C=[float(i) for i in B] #Float conversion.
    D[0].append(C[0]),D[1].append(C[1]),D[2].append(C[2])
    for i in range(4, 12):
        if NC >= i:
            D[i - 1].append(C[i - 1])
    if NC == 12:
        D[11].append(C[11])

EDIT: I know you mentioned that this is working for your purposes, but be aware of what happens for the case when NC > 12. That means you will append to everything up to but not including D[11]. If that's what you want, then you're golden. But if that last comparison is really supposed to be >= rather than ==, then you can of course just write the program as

    for i in range(4, 13):
        if NC >= i:
            D[i - 1].append(C[i - 1])

Without that extra if NC == 12 at the end.