我有这个:
l_string = """[[-0.5, '#572fa5', 1], [-0.25, "#70339b", 1], [0.2, "#883691", 0.3], [0.5, "#b83d7e", 1], ["#e9446a", 1]]"""
and I want
l = [[-0.5, '#572fa5', 1], [-0.25, "#70339b", 1], [0.2, "#883691", 0.3], [0.5, "#b83d7e", 1], [1, "#e9446a", 1]]
如何转换?
您需要使用eval()
将字符串解析为有效的Python列表:
l_string = """[[-0.5, '#572fa5', 1], [-0.25, "#70339b", 1], [0.2, "#883691", 0.3], [0.5, "#b83d7e", 1], ["#e9446a", 1]]"""
l_string = eval(l_string)
print(l_string)
输出:
[[-0.5, '#572fa5', 1], [-0.25, '#70339b', 1], [0.2, '#883691', 0.3], [0.5, '#b83d7e', 1], ['#e9446a', 1]]