我有一个包含此列表的 python 脚本:
blocks = [
"item-1",
"item-2",
"item-3.0;item-3.1;item-3.2"
]
Run Code Online (Sandbox Code Playgroud)
我试过这个:
for (i, block) in enumerate(blocks):
if ";" in block:
[blocks.insert(i, c) for c in block.split(";")]
else:
blocks.insert(i, block)
Run Code Online (Sandbox Code Playgroud)
要得到这个:
blocks = [
"item-1",
"item-2",
"item-3.0",
"item-3.1",
"item-3.2"
]
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的代码不断覆盖列表中的元素,我只剩下这个:
blocks = [
"item-1",
"item-2",
"item-3.2"
]
Run Code Online (Sandbox Code Playgroud)
如何修改脚本以允许我在列表中拆分字符串,然后将新的子字符串插入原始字符串的位置,而不会覆盖列表中的其他元素?