如何在python中扩展字符串中的字符串?

pra*_*man 7 python string dictionary list-comprehension concatenation

我有一个看起来像这样的字符串:

1 | xxx | xxx | xxx | yyy*a*b*c | xxx
Run Code Online (Sandbox Code Playgroud)

我想扩展该yyy*a*b*c部分,以便字符串看起来像这样:

1 | xxx | xxx | xxx | yyya | yyyb | yyyc | xxx
Run Code Online (Sandbox Code Playgroud)

我实际上有一个大文件,在这些字符串之间有一个分隔符.我已将文件解析为一个如下所示的字典:

{'1': ['xxx' , 'xxx', 'xxx', 'yyy*a*b*c', 'xxx' ], '2': ['xxx*d*e*f', ...,  'zzz'], etc}
Run Code Online (Sandbox Code Playgroud)

我需要有一个yyy*a*b*cxxx*d*e*f部分与列表中的其他项目所取代.

我怎么能在python 3中做到这一点?在将其解析为字典或将其解析为字典(以及如何)之后,我是否应该扩展字符串中的所有内容?

Tua*_*-Vu 1

您可以使用拆分和简单列表理解来做到这一点:

def expand_input(input):
    temp = input.split("*")
    return [temp[0]+x for x in temp[1:]]

print(expand_input("yyy*a*b*c"))
>>> ['yyya', 'yyyb', 'yyyc']
Run Code Online (Sandbox Code Playgroud)