2013-11-27 45 views

回答

12

Python帶有電池包括 - 這個問題是由ast.literal_eval()解決:

>>> import ast 
>>> ast.literal_eval("[(0, 1), (1, 3), (2, 1), (3, 1), (4, 1)]") 
[(0, 1), (1, 3), (2, 1), (3, 1), (4, 1)] 
>>> ast.literal_eval("[(0, 1, 6), (1, 3,7), (3, 1,4), (3, 1,3), (8, 1,2)]") 
[(0, 1, 6), (1, 3, 7), (3, 1, 4), (3, 1, 3), (8, 1, 2)] 
>>> ast.literal_eval("[1,2,3,5,3]") 
[1, 2, 3, 5, 3] 
+0

完美,謝謝!!!!! – alvas

+2

@alvas:請注意,'ast.literal_eval'是安全的,而'eval'則不是。 –