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

如何拆分列表以排除括号、逗号及引号,提取独立单词

拆分句子为不含标点的独立单词

可以通过以下几种方法实现,以Python为例:

方法一:正则表达式匹配

利用正则表达式直接提取所有不含标点的单词,是最简洁的方式:

import re

# 示例句子列表
sentence_list = ["Hello, world!", "This is a 'test' (example).", "我是,一个'测试'(句子)。"]
all_words = []

for sentence in sentence_list:
    # 匹配英文单词或中文词语,自动忽略标点
    matched_words = re.findall(r'[\u4e00-\u9fa5a-zA-Z]+', sentence)
    all_words.extend(matched_words)

print(all_words)
# 输出:['Hello', 'world', 'This', 'is', 'a', 'test', 'example', '我是', '一个', '测试', '句子']

方法二:手动过滤标点后拆分

先定义需要排除的标点集合,清理句子后再拆分单词:

# 定义要移除的标点符号
exclude_puncts = {',', "'", '(', ')', '"', '!', '。', ','}

sentence_list = ["Hello, world!", "This is a 'test' (example).", "我是,一个'测试'(句子)。"]
all_words = []

for sentence in sentence_list:
    # 移除句子中的目标标点
    cleaned_sent = ''.join([char for char in sentence if char not in exclude_puncts])
    # 按空格拆分得到单词
    all_words.extend(cleaned_sent.split())

print(all_words)
# 输出:['Hello', 'world', 'This', 'is', 'a', 'test', 'example', '我是', '一个', '测试', '句子']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:13:20