1

I'm trying to convert a given input to a list containing lists as shown in the command-line code below:

matrix = input('asking')
asking[[2, 0, 4],[1, 2, 4],[4, 4, 2]]
matrix
'[[2, 0, 4],[1, 2, 4],[4, 4, 2]]'

desired output:

[[2,0,4],[1,2,4],[4,4,2]]

Attempts

list(matrix)
['[', '[', '2', ',', ' ', '0', ',', ' ', '4', ']', ',', ' ', '[', '1', ',', ' ', '2', ',', ' ', '4', ']', ',', ' ', '[', '4', ',', ' ', '4', ',', ' ', '2', ']', ']']

x = [int(a) for a in matrix] 
builtins.TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

matrix.split(',')
['[[2', '0', '4]', '[1', '2', '4]', '[4', '4', '2]]']

Code:

fin_result = 0

def perform_check():
    print("Enter form \n [[a,b,c] \n [d,e,f] \n [g,h,i]]")
    #ask_for_input = [[4,-1,1],[4,5,3],[-2,0,0]]
    ask_for_input = [[2,0,4],[1,2,4],[4,4,2]]
    print(type(ask_for_input))

    #call function to cont
    calculate_determinate_matrix(ask_for_input)


def calculate_determinate_matrix(matrix3x3):
    matrix_list2x2 = []

    matrix_list2x2.append([[matrix3x3[1][1], matrix3x3[1][2]], [matrix3x3[2][1], matrix3x3[2][2]]])

    matrix_list2x2.append([[matrix3x3[1][0], matrix3x3[1][2]],[matrix3x3[2][0], matrix3x3[2][2]]])

    matrix_list2x2.append([[matrix3x3[1][0], matrix3x3[1][1]],[matrix3x3[2][0], matrix3x3[2][1]]])

    count = 0
    for count, matrix_2x2 in enumerate(matrix_list2x2):
        if count % 2 == 1:
            calculate_2x2_matrix(matrix_2x2, matrix3x3[0][count] * -1)
        else:
            calculate_2x2_matrix(matrix_2x2, matrix3x3[0][count])

def calculate_2x2_matrix(matrix, mult):
    global fin_result
    result = matrix[0][0] * matrix[1][1]
    result_2 = matrix[0][1] * matrix[1][0]
    fin_result += mult * (result - result_2)    

def main():
    perform_check()
    print(fin_result)

if __name__ == "__main__":
    main()

Clearly this code needs work, but I can't figure this in terms of list comprehension.

2 Answers 2

2

Use json loads() method:

matrix = '[[2, 0, 4],[1, 2, 4],[4, 4, 2]]'
matrix = json.loads(matrix)
type(matrix)
<type 'list'>

And in case you want to check for errors in input you can wrap it in try-except:

try:
    matrix = json.loads(matrix)
except ValueError:
    #code goes here
Sign up to request clarification or add additional context in comments.

2 Comments

Beautiful :) Thank you very much. Didn't even know that library existed.
You are welcome :) FYI accepting an answer will increase your reputation.
0

An alternative to json.loads is ast.literal_eval. It has the advantage that it recognizes Python syntax even if it was invalid JSON, like single-quoted strings. In your exact case, it doesn't make a difference:

import ast
matrix = '[[2, 0, 4],[1, 2, 4],[4, 4, 2]]'
matrix = ast.literal_eval(matrix)

2 Comments

Thanks Zipa was quika though :P I don't think i understand the reason for using ast.literal_eval yet, i will have a fiddle.
In your case, use json.loads. But let's say you want a list of strings, e.g. ['foo', 'bar', 'bat']. This will not work with json.loads (although ["foo", "bar", "bat"] will), only with ast.literal_eval. I know zipa's answer is the right solution for you, but StackOverflow is as much for the person asking the questions as it is for those coming here by googling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.