[英]convert a list of strings that i would like to convert to a list of tuples
i have a list of strings that i would like to convert to a list of tuples. 我有一个字符串列表,我想将其转换为元组列表。 Below is an example.
下面是一个例子。
['(0, "ass\'")', "(-1, '\\n print self.amount')", "(0, '\\n\\n ')"]
to be converted to. 转换为。
[(0, "ass\'"), (-1, '\\n print self.amount'), (0, '\\n\\n ')]
any ideas? 有任何想法吗?
[ast.literal_eval(x) for x in L]
map(ast.literal_eval, list_of_tuple_strings)
与eval
不同,ast.literal_eval将仅评估文字,而不评估函数调用,因此更加安全。
The function eval is what you need I think, but be careful with its use: 我认为功能eval是您所需要的,但请谨慎使用:
>>> l = ['(0, "ass\'")', "(-1, '\\n print self.amount')", "(0, '\\n\\n ')"]
>>> map(eval, l)
[(0, "ass'"), (-1, '\n print self.amount'), (0, '\n\n ')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.