Python——如何将字符串合并成列表
在开发应用程序时,有很多场景需要对字符串进行操作并将其转换为可变数据结构,比如列表。
举例
# Importing ast library import ast # Initialization of strings str1 ="'Python', 'for', 'fun'" str2 ="'vishesh', 'ved'" str3 ="'Programmer'" # Initialization of list list = [] # Extending into single list for x in (str1, str2, str3): list.extend(ast.literal_eval(x)) # printing output print(list) # using eval # Initialization of strings str1 ="['python, 'for', ''fun']" str2 ="['vishesh', 'ved']" str3 ="['programmer']" out = [str1, str2, str3] out = eval('+'.join(out)) # printing output print(out)
广告