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

如何用Python提取长字符串中的小写字符块并存入列表?

提取字符串中连续小写字符块的方法

直接用Python的正则表达式模块re就能轻松实现,核心思路是匹配所有连续的小写字母段:

import re

# 你的输入字符串
input_str = "ATGCAATTAATTGCatgcatgcatgcAAAATTTGGGGCCCCccggttaaatcggatcTGTGCACACTCTAGAGaaaaaaaaattttttt"
# 提取所有连续小写字符块
lowercase_blocks = re.findall(r'[a-z]+', input_str)
# 输出结果
print(lowercase_blocks)

代码说明

  • r'[a-z]+'是正则匹配规则:[a-z]匹配任意单个小写英文字母,+表示匹配一个及以上连续的小写字母;
  • re.findall()会把所有符合规则的匹配结果收集成列表返回,正好满足需求。

运行后输出结果就是:

['atgcatgcatgc', 'ccggttaaatcggatc', 'aaaaaaaaattttttt']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:29:57