3 回答

TA貢獻1780條經(jīng)驗 獲得超1個贊
你可以ast.literal_eval()用來做你的出價:
import ast
s = ast.literal_eval("[1,2,3]")
s == [1,2,3]

TA貢獻1859條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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
添加回答
舉報