Generating variables to be named, filled in and operated on the fly in Python I am very new to Python and trying to refactor my code which is ripe for error.

Currently, I manually name a bunch of arrays:

open_cost_mat_train = np.array([])
closed_cost_mat_train = np.array([])
open_cost_mat_train_1 = np.array([])
closed_cost_mat_train_1 = np.array([])
open_cost_mat_train_2 = np.array([])
closed_cost_mat_train_2 = np.array([])


I then pass them to a function that fills the arrays in differently according to their number (e.g. _1, _2, and also if their name contains open or closed):

for i in xrange(10):    
    open_cost_mat_train = np.append(open_cost_mat_train,i)
    open_cost_mat_train_1 = np.append(open_cost_mat_train_1,i/3))
    open_cost_mat_train_2 = np.append(open_cost_mat_train_2,i*64))
    closed_cost_mat_train = np.append(closed_cost_mat_train,i*3)
    closed_cost_mat_train_1 = np.append(closed_cost_mat_train_1,i/5))
    closed_cost_mat_train_2 = np.append(closed_cost_mat_train_2,i*102))


I then generate output file paths which corresponds with those matrices:

open_cost_category_path = "openCostClassifier.dat"
closed_cost_category_path = "closedCostClassifier.dat"
open_cost_category_path_1 = "openCostClassifier_1.dat"
closed_cost_category_path_1 = "closedCostClassifier_1.dat"
open_cost_category_path_2 = "openCostClassifier_2.dat"
closed_cost_category_path_2 = "closedCostClassifier_2.dat"


And finally I pass those variables to a new function (not necessary to show this for sake of question:

csoaa_open = example(open_cost_mat_train,open_cost_category_path)
csoaa_closed = example(closed_cost_mat_train,closed_cost_category_path)
csoaa_open_1 =example(open_cost_mat_train_1,open_cost_category_path_1)
csoaa_closed_1 = example(closed_cost_mat_train_1,closed_cost_category_path_1)
csoaa_open_2 = example(open_cost_mat_train_2,open_cost_category_path_2)
csoaa_closed_2 = example(closed_cost_mat_train_2,closed_cost_category_path_2)


What am I doing wrong with my code and how can I improve how I do things? Because this is the way I have written a lot of my code and getting one variable wrong. Doing this, for instance, will mess up the whole of my code:

csoaa_closed_1 = example(closed_cost_mat_train_2,closed_cost_category_path_1)
Just a little something to clean up the code.

The lines inside your for loop e.g

open_cost_mat_train = np.append(open_cost_mat_train,i)


do not require an assignment on the left, np.append modifies the array being passed in. I'm not sure if that's what you want, but maybe you should inspect the arrays after the loop to ensure they contain what you expect.

If you're worried about modifying arrays by mistake or require a original copy, it may be worth cloning them before running any operations on them. 

copy_array = original_array.copy() will clone a numpy array. 

Another way to verify you're passing the right parameters could be to check their names. They seem to be labelled logically with a number at the end, so it seems closed_cost_mat_train_2 should always go with closed_cost_category_path_2. See here, the inspect module looks like a good choice for this.

Docs for the numpy functions:

[1] numpy.append 

[2] numpy.copy