-1

I want to convert this whole string into a 2d list of integer type.

b="[[31, 91, 80, 11], [50, 11, 20, 10], [22, 33, 11, 11], [10, 12, 33, 23], [8, 8, 8, 8]]"

I tried with the isdigit() function but it wouldn't work for the double digit strings as it was also splitting it into two parts like I need 31 as a single integer but isdigit() gives my output 3 1 separately.

for i in b:
    if (i.isdigit() == True):

Please help me to get out of this issue and solve my problem. so that it shows output as :

[[31, 91, 80, 11], [50, 11, 20, 10], [22, 33, 11, 11], [10, 12, 33, 23], [8, 8, 8, 8]]

1 Answers1

0

You can use literal_eval from ast.

import ast
b="[[31, 91, 80, 11], [50, 11, 20, 10], [22, 33, 11, 11], [10, 12, 33, 23], [8, 8, 8, 8]]"
ast.literal_eval(b)

Output:

[[31, 91, 80, 11], [50, 11, 20, 10], [22, 33, 11, 11], [10, 12, 33, 23], [8, 8, 8, 8]]
venky__
  • 5,552
  • 3
  • 17
  • 28
  • You should mark an answer as "answered" if it helps you. green tick on left side below the upvote buttons. https://stackoverflow.com/help/someone-answers – venky__ Sep 14 '20 at 10:54