You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 如何从列表的字母数字混合字符串中剔除所有数字

Python实现字符串列表批量删除数字的实现方案

注意:不要使用list作为自定义变量名,它是Python内置的列表类型关键字,会覆盖原生功能导致后续报错。

可以选择以下两种常用实现方式:

  • 方法1:列表推导式+str.isdigit()字符筛选
    逻辑是遍历每个字符串的单个字符,仅保留非数字字符后重新拼接,无需引入第三方依赖:

    original_list = ["defew21", "dldapl43", "vdvc3232"]
    result_list = [''.join(c for c in s if not c.isdigit()) for s in original_list]
    print(result_list)
    # 输出结果:['defew', 'dldapl', 'vdvc']
    
  • 方法2:正则表达式批量替换
    用正则匹配所有数字字符直接替换为空,处理长文本/大列表时性能更优:

    import re
    original_list = ["defew21", "dldapl43", "vdvc3232"]
    result_list = [re.sub(r'\d', '', s) for s in original_list]
    print(result_list)
    # 输出结果:['defew', 'dldapl', 'vdvc']
    

内容的提问来源于stack exchange,提问作者ronnie900

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 03:36:00