将字符串转换为元组列表 [英] converting a string to a list of tuples
本文介绍了将字符串转换为元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Python中将字符串'(2,3,4),(1,6,7)'
转换为元组[(2,3,4),(1,6,7)]
的列表.
我当时想在每个','
处拆分,然后使用for循环并将每个元组附加到一个空列表.但是我不太确定该怎么做.
提示,有人吗?
I need to convert a string, '(2,3,4),(1,6,7)'
into a list of tuples [(2,3,4),(1,6,7)]
in Python.
I was thinking to split up at every ','
and then use a for loop and append each tuple to an empty list. But I am not quite sure how to do it.
A hint, anyone?
推荐答案
>>> list(ast.literal_eval('(2,3,4),(1,6,7)'))
[(2, 3, 4), (1, 6, 7)]
这篇关于将字符串转换为元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文