3 回答

TA贡献1780条经验 获得超1个赞
你可以ast.literal_eval()用来做你的出价:
import ast
s = ast.literal_eval("[1,2,3]")
s == [1,2,3]

TA贡献1859条经验 获得超6个赞
您可以json在某些情况下使用,但@Bharel 的回答更好
import json
with open('data-string.txt') as f:
lst = json.load(f)
print(lst) # [[1, 2, 3], [2, 3, 4], [3, 4, 5]]

TA贡献1836条经验 获得超3个赞
这个怎么样:
// convert original string to type list
str1 = list("[[1, 2, 3], [2, 3, 4], [3, 4, 5]]")
// create a new string using the following, which converts same to a 3 x 3 list
str2 = ''.join(str(e) for e in str1)
// prints out as [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
// https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string
添加回答
举报