-1

How to convert this string to dict in python ?

[[[0,1,2,2,3,3,3,3,3,4,4,4,5,5,6,6,6,5],  [1,0,1,1,2,2,2,2,2,3,3,3,4,4,5,5,5,4],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,0,0,0,0]]]

it supposed to be like this

[[0,1,2,2,3,3,3,3,3,4,4,4,5,5,6,6,6,5],  [1,0,1,1,2,2,2,2,2,3,3,3,4,4,5,5,5,4],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,0,0,0,0]]
martineau
  • 119,623
  • 25
  • 170
  • 301
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

1 Answers1

3

Not sure where the dictionary is coming into play, but you can do something like this to get the desired output:

from ast import literal_eval

arr_string = '[[[0,1,2,2,3,3,3,3,3,4,4,4,5,5,6,6,6,5],  [1,0,1,1,2,2,2,2,2,3,3,3,4,4,5,5,5,4],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,0,0,0,0]]]'

result = [arr for arr in literal_eval(arr_string)[0]]
print(result)   # [[0,1,2,2,3,3,3,3,3,4,4,4,5,5,6,6,6,5],  [1,0,1,1,2,2,2,2,2,3,3,3,4,4,5,5,5,4],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [2,1,0,0,1,1,1,1,1,2,2,2,3,3,4,4,4,3],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [3,2,1,1,0,0,0,0,0,1,1,1,2,2,3,3,3,2],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [4,3,2,2,1,1,1,1,1,0,0,0,1,1,2,2,2,1],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,1,1,1,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [6,5,4,4,3,3,3,3,3,2,2,2,1,1,0,0,0,0],  [5,4,3,3,2,2,2,2,2,1,1,1,0,0,0,0,0,0]]


Dor-Ron
  • 1,787
  • 3
  • 15
  • 27