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)热门推荐
10 婚礼仪式祝福语简短
11 给导师送花祝福语简短
12 姥爷生日祝福语简短精辟
13 送礼祝福语男性生日简短
14 祝福语简短七夕
15 哥哥病了祝福语大全简短
16 白羊生日祝福语 简短独特
17 朋友结婚致辞祝福语简短
18 宝宝生病简短祝福语大全