Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将此字符串转换为整数数组?_Python_String_Dictionary - Fatal编程技术网

Python 如何将此字符串转换为整数数组?

Python 如何将此字符串转换为整数数组?,python,string,dictionary,Python,String,Dictionary,如何在python中将此字符串转换为dict [[[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,

如何在python中将此字符串转换为dict

[[[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]]]
应该是这样的

[[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]]

不确定字典将在何处发挥作用,但您可以这样做以获得所需的输出:

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,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],  [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]]



你能详细说明你的问题吗,我看不出你要创建的命令在哪里第一项是字符串,我需要它是int数组的数组你想进一步展平现有的列表吗?第一项,如你的问题中所示,是int数组的数组-或者用python术语来说,是列表的列表,而不是字符串。我想在矩阵上使用旅行商贪婪算法,数据以字符串的形式出现,我不知道如何在矩阵中读取它