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

非正则表达式实现标点拆分与单词拼接的代码调试问题

Let's break down what's wrong with your current code and fix it step by step to meet your requirement.

What's Wrong with Your Current Code

  • Incorrect splitting logic: Using sentence.split('_') splits the entire input at every underscore—including the spaces around them. For your input, this creates ["Democr ", "acy , is overrat ", "ed ."], and joining these gives messy text with extra spaces instead of properly combining word fragments.
  • Misplaced, useless condition checks: The variable c starts as an empty string, so c not in punctuation is always true. The elif block never runs unless you modify c—and even if it did, looping through every character in the sentence and splitting on each c would just spam random lists, which explains your 29-list output.
  • No punctuation spacing handling: Your code doesn't address the core need to attach punctuation directly to the preceding word (like turning acy , into acy,).

Step-by-Step Fix

We'll skip regex entirely and focus on two key tasks: combining word fragments separated by _ (space-underscore-space), and cleaning up punctuation spacing to match your expected output.

Here's the corrected code:

sentence = input().strip()
# Define all punctuation marks we need to adjust
punctuation = {"!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~"}

# Step 1: Merge word fragments split by " _ "
processed = sentence.replace(" _ ", "")

# Step 2: Remove spaces before punctuation to attach them to words
for p in punctuation:
    processed = processed.replace(f" {p}", p)

print(processed)

How It Works

  1. Combining word fragments: The replace(" _ ", "") call targets the exact pattern separating your word parts. For your input, Democr _acy becomes Democracy and overrat _ed becomes overrated.
  2. Cleaning punctuation: We loop through each punctuation mark and replace any instance of [space][punctuation] with just the punctuation. This fixes acy , to acy, and ed . to ed. while leaving normal spaces between words (like is overrated) intact.

Testing with your input Democr _acy , is overrat _ed . gives the expected output:

Democracy, is overrated.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 19:33:13