convert string to matrix python code example

Example 1: how to convert a matrix string back to a matrix python

from ast import literal_eval as safe_eval

string = '[[396, 555], [326, 337], [467, 332]]'
print(safe_eval(string))

# will return: [[396, 555], [326, 337], [467, 332]]

Example 2: convert matrix string to matrix python

arr = '[[1, 2], [3, 4], [5, 6]]'
num_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
arr_list = list(arr)

matrix = []
row = []

for i in range(0, len(arr_list)):
    try:
        if(len(row) == 2):
            matrix.append(row)
            row = []
        if(int(arr_list[i]) in num_set):
            row.append(int(arr_list[i]))
    except:
        continue

print(matrix)