首页 > 解决方案 > 从字符串中删除前缀

问题描述

在 python 中执行此操作的可爱方法是什么?

假设我们有一个字符串列表:

clean_be
clean_be_al
clean_fish_po
clean_po

我们希望输出为:

be
be_al
fish_po
po

标签: python

解决方案


另一种适用于所有场景的方法:

import re
data = ['clean_be',
'clean_be_al',
'clean_fish_po',
'clean_po', 'clean_a', 'clean_clean', 'clean_clean_1']
for item in data:
    item = re.sub('^clean_', '', item)
    print (item)

输出:

be
be_al
fish_po
po
a
clean
clean_1

推荐阅读