Convert data to multiple dictionaries

advertisements

I have a data matrix (106 rows and 57 columns), where all the data is ['a','c','g','t'], and 1 dictionary per column too. What I need to do is apply a transformation for each column based on its own dictionary.

I tried something like this, but the results are not correct.

result = []
for x in range(data.shape[0]):
    individual_map = np.empty(data.shape[1], dtype=int)

    for y in range(data.shape[1]):
        for key, val in mapping[y].items():
            individual_map[data[x,y] == key] = val

    result.append(individual_map)

Thanks.

EDIT: The sample data has 3 columns only, so I just have 3 dictionaries. In the real case, there are 57 columns and 57 dictionaries. Sample data:

data = [['a','c','g'],['t','g','c']]
dictionaries = [{'a':1,'c':2,'g':3,'t':4},{'a':3,'c':1,'g':2,'t':4},{'a':2,'c':3,'g':4,'t':1}]
result = [[1,1,4],[4,2,3]]


If you have,

data = [['a','c','g'],['t','g','c']]
dictionaries = [{'a':1,'c':2,'g':3,'t':4},{'a':3,'c':1,'g':2,'t':4},{'a':2,'c':3,'g':4,'t':1}]

and the expected result is

result = [[1,1,4],[4,2,3]]

you can get it with two nested list comprehensions (https://docs.python.org/2/tutorial/datastructures.html):

result = [[dictionaries[j][item] for j, item in enumerate(row)] for row in data]