I represent a Matrix by a dictionary and assuming that I have a dictionary that the keys are the row indices, how can I convert it (in-place seems tricky) to a dictionary that the keys will be the column indices?
Here is my attempt:
by_row = {}
N = 3
M = 5
for i in range(1, N):
row = []
for j in range(1, M):
row.append(j)
by_row[i] = row
print by_row
by_column = {}
for i in range(1, M):
col = []
for j in range(1, N):
col.append(by_row[j][i - 1])
by_column[i] = col
print by_column
but I am looking for something more elegant and Pythonic. Here my_dict_1
has the row indices as keys, while my_dict_2
has the column indices as keys. Output:
{1: [1, 2, 3, 4], 2: [1, 2, 3, 4]}
{1: [1, 1], 2: [2, 2], 3: [3, 3], 4: [4, 4]}