Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 5 characters in body
Source Link
Unheilig
  • 16.3k
  • 193
  • 71
  • 101

toTo convert a stringstring having the form a="[[1, 3], [2, -6]]"a="[[1, 3], [2, -6]]" I wrote yet not optimized code:

matrixAr = [] mystring = "[[1, 3], [2, -4], [19, -15]]" b=mystring.replace("[[","").replace("]]","") # to remove head [[ and tail ]] for line in b.split('], ['): row =list(map(int,line.split(','))) #map = to convert the number from string (some has also space ) to integer matrixAr.append(row) print matrixAr 

to convert a string having the form a="[[1, 3], [2, -6]]" I wrote yet not optimized code

matrixAr = [] mystring = "[[1, 3], [2, -4], [19, -15]]" b=mystring.replace("[[","").replace("]]","") # to remove head [[ and tail ]] for line in b.split('], ['): row =list(map(int,line.split(','))) #map = to convert the number from string (some has also space ) to integer matrixAr.append(row) print matrixAr 

To convert a string having the form a="[[1, 3], [2, -6]]" I wrote yet not optimized code:

matrixAr = [] mystring = "[[1, 3], [2, -4], [19, -15]]" b=mystring.replace("[[","").replace("]]","") # to remove head [[ and tail ]] for line in b.split('], ['): row =list(map(int,line.split(','))) #map = to convert the number from string (some has also space ) to integer matrixAr.append(row) print matrixAr 
Source Link

to convert a string having the form a="[[1, 3], [2, -6]]" I wrote yet not optimized code

matrixAr = [] mystring = "[[1, 3], [2, -4], [19, -15]]" b=mystring.replace("[[","").replace("]]","") # to remove head [[ and tail ]] for line in b.split('], ['): row =list(map(int,line.split(','))) #map = to convert the number from string (some has also space ) to integer matrixAr.append(row) print matrixAr