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)热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿