python - 从字符串中删除前缀
问题描述
在 python 中执行此操作的可爱方法是什么?
假设我们有一个字符串列表:
clean_be
clean_be_al
clean_fish_po
clean_po
我们希望输出为:
be
be_al
fish_po
po
解决方案
另一种适用于所有场景的方法:
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
推荐阅读
- python - 为什么我不能从列表中删除成员并删除他们的兴趣?
- javascript - CSS 绝对和相对不适用于我的 div 滑块轮播
- visual-c++ - 如何在报表视图中显示我的列表控件。我想查看关于列表控件 Visual Studio 2013 更新 4 属性的视图
- ruby-on-rails - Sidekiq-Unique-jobs Lock 仅适用于第一个 Worker,然后所有待处理的作业同时执行?
- c - 以正确的顺序打印结果链表
- pine-script - 如何优化下面的代码,召回太多类似的功能?
- android - 如何在适配器中获得 WRITE_EXTERNAL_STORAGE 权限?
- c# - 如何使用数组计算模式。我只是编程的初学者。它用于我们的决赛项目
- go - 如何从将 x-real-ip 和 x-forward-for 添加到标头的负载均衡器中获取 gRPC 中的客户端 IP 地址?
- c# - 如何在 C# Automapper 中忽略空值