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 红包祝福语简短10字
18 周六早晨祝福语简短