如果我有一个可以附加到字符串的前缀列表,我如何将一个字符串拆分为它的前缀和下一个子字符串中的其他字符。例如:
prefixes = ['over','under','re','un','co']
str1 = "overachieve"
output: ["over","achieve"]
str2 = "reundo"
output = ["re","un","do"]
有没有更好的方法来完成上述任务,也许使用正则表达式或一些字符串函数,而不是:
str1 = "reundo"
output = []
for x in [p for p in prefixes if p in str1]:
output.append(x)
str1 = str1.replace(x,"",1)
output.append(str1)