Python下怎样将字符串类型的列表转换为列表对象??

Python下怎样将字符串类型的列表转换为列表对象??????

已邀请:

koyo - 网站管理 致力做全栈工程师 爱ui 爱前端

赞同来自: being

from ast import literal_eval

str_tuple = "(1, 2, 3, 4)"
str_list = "[1, 2, 3, 4]"
str_set = "{1, 2, 3, 4}"
str_dict = "{'name': 'lucky', 'age': 18, 'job': 'Dev'}"

tuple_info = literal_eval(str_tuple)
list_info = literal_eval(str_list)
set_info = literal_eval(str_set)
dict_info = literal_eval(str_dict)
print(' tuple_info type: %s\n list_info type: %s\n set_info type: %s\n dict_info type %s' %
(type(tuple_info), type(list_info), type(set_info), type(dict_info))
)

being - 学习不断,技术不断

要回复问题请先登录注册