import numpy as np
from itertools import count
data = [line.split() for line in inputfile.readlines()]
rows = dict(zip(sorted(set(line[0] for line in data)), count()))
cols = dict(zip(sorted(set(line[1] for line in data)), count()))
array = np.zeros((len(rows), len(cols)))
for row, col, val in data:
index = (rows[row], cols[col])
array[index] = val
I don't know how to mark rows and columns in numpy, so I just made a dict matching the row label with the row index, and the other the same for the columns. If you need it, you can make a reverse map, as shown below, or you can make rows and columns of bidict .
rows_reverse = dict((v, k) for k, v in rows)
cols_reverse = dict((v, k) for k, v in cols)
source
share