Move from an array of 'labels' to array of equations corresponding to those labels in Python 2.7
Question: How can I **efficiently** move from an array of essentially labels to
equations corresponding to those labels in python 2.7?

This image illustrates what I mean:

![](https://i.imgur.com/fmGuL2D.png)

The equations pull values from array x, so "xn" in the image in python terms
would be x[n]. As an example, the 0th entry in the label array is "a" which
corresponds to the equation 1+xn which would be 1+x[0]. The next is "b" which
is x[1]*2.

There are a plenty of ways to accomplish this, but I want to focus on
efficiency. The actual arrays have thousands of elements, and this operation
is performed thousands of times (each timestep in my model). The x array will
be different at each timestep. What I am actually doing is building the
diagonals for a tridiagonal matrix based on various boundary conditions.

Can anyone offer some insight better than iterating through the whole array
each timestep with a switch case?

Here is an example of me doing it a brute way:

    
    
    '''
    Equations corresponding to various labels
    
    a -> 2+x[n]
    b -> 3*x[n-1]+2x[n]
    c -> 4*x[n]
    
    These are just dummy equations I am making up for the example
    '''
    
    x = [4,7,6,6,9,12,4,9,1,11]
    
    labelArray = ['a','b','b','b','c','c','a','b','b','c']
    
    outputArray = []
    n = 0
    for label in labelArray:
        if label == 'a':
            output = 2+x[n]
        elif label == 'b':
            output = 3*x[n-1]+2*x[n]
        elif label == 'c':
            output = 4*x[n]
        outputArray.append(output)
        n += 1
    
    print outputArray
    # outputArray = [6, 26, 33, 30, 36, 48, 6, 30, 29, 44]
    


Answer: Here's a solution I came up with. It should be faster and more concise, though
it may not be the optimal solution.

    
    
    from itertools import imap
    
    # List of values
    values = [4, 7, 6, 6, 9, 12, 4, 9, 1, 11]
    
    # A list of corresponding methods for each value, must be same length as values.
    # Optionally, you could create the data with the value and method in a tuple
    # e.g. [(4, 'a'), (7, 'b') ... (x, 'y')]
    # Though if you ensure both lists are of the same length, you can use the zip()
    # method, which does the same thing.
    methods = ['a', 'b', 'b', 'b', 'c', 'c', 'a', 'b', 'b', 'c']
    
    # A dictionary with all your equations. You can also define them in a function
    # elsewhere and include them like 
    # >{ 'a': external_function }
    equations = {
        # Lambda is just an anonymous function.
        'a': lambda index: 2 + values[index],
        'b': lambda index: 3 * values[index-1] + 2 * values[index],
        'c': lambda index: 4 * values[index],
    }
    
    
    # Returns an iterator to prevent eating up your memory with one big array.
    new_values = imap(lambda x,y: equations[x](y), methods, xrange(len(values)))
    print [value for value in new_values]
    

Check out <https://docs.python.org/2/library/functions.html> for an
explanation of the built in methods I'm using here. Here's some info on
iterators: <http://anandology.com/python-practice-book/iterators.html>

