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

动态长度字符串文件匹配:Python代码格式与计数错误修正求助

问题:修正变体字符串搜索的Python代码

需求

以字符串"Simultaneously"生成指定变体(如下列列表),在目标文件中搜索这些变体,按指定格式输出每个匹配字符串对应的所有匹配行及出现次数。

搜索变体列表

Simltaneously
Simtaneously
Simaneously
Simneously
Simeously
Simously
Simusly
Simsly

目标文件(Sequences2.txt)内容

Tiled say decay **Simeously** spoil now walls meant house. 
My mr interest thoughts **Simeously** screened of outweigh removing. 
Lose hill well up will he over on. 
Increasing **Simeously** sufficient everything men him admiration unpleasing. 
Around really his use uneasy longer him man. 
His our **Simeously** pulled nature elinor talked now for excuse result. 
Wicket longer admire do barton vanity itself do in it. 
Preferred to **Simaneously** sportsmen it engrossed listening. 
Park gate sell they west hard for the. 
Up colonel so between removed **Simtaneously** so do. 
Years use place decay sex worth drift age. 
Men lasting out end **Simtaneously** article express fortune demands own charmed.

预期输出格式

Simaneously
Preferred to Simaneously sportsmen it engrossed listening. 
Occurrence of Simaneously is: 1

Simeously
Tiled say decay Simeously spoil now walls meant house. 
My mr interest thoughts Simeously screened of outweigh removing. 
Increasing Simeously sufficient everything men him admiration unpleasing. 
His our Simeously pulled nature elinor talked now for excuse result. 
Occurrence of Simeously is: 4

Simtaneously
Up colonel so between removed Simtaneously so do. 
Men lasting out end Simtaneously article express fortune demands own charmed.
Occurrence of Simtaneously is: 2

尝试的错误代码

word = "Simultaneously"
count = 0
res_word = ""

with open("Sequences2.txt", 'r') as seq:
    lines = seq.readlines()
    for i in range(3, len(word)-3):
        res_word = "Sim" + word[i+1:]
        for line in lines:
            if res_word in line:
                count = count + 1
                print(line)
                print("Occurrence of", res_word, "is:", count)
                print(res_word)
        i = i + 1

当前错误输出

Simtaneously
Up colonel so between removed Simtaneously so do. 

Occurrence of Simtaneously is: 1
Simtaneously
Men lasting out end Simtaneously article express fortune demands own charmed.
Occurrence of Simtaneously is: 2
Simaneously
Preferred to Simaneously sportsmen it engrossed listening. 

Occurrence of Simaneously is: 3
Simeously
Tiled say decay Simeously spoil now walls meant house. 

Occurrence of Simeously is: 4
Simeously
My mr interest thoughts Simeously screened of outweigh removing. 

Occurrence of Simeously is: 5
Simeously
Increasing Simeously sufficient everything men him admiration unpleasing. 

Occurrence of Simeously is: 6
Simeously
His our Simeously pulled nature elinor talked now for excuse result. 

Occurrence of Simeously is: 7

问题分析

  1. 计数错误:全局变量count未按变体重置,导致所有变体的计数累加,而非每个变体单独统计。
  2. 输出格式混乱:每找到一行就打印计数和变体名称,不符合先展示所有匹配行再输出计数的要求。
  3. 变体生成逻辑偏差:原代码通过截取字符串生成的变体可能和用户指定的列表不一致,导致搜索范围错误。

修正后的代码

# 定义需要搜索的变体列表,确保和需求一致
variants = [
    "Simltaneously",
    "Simtaneously",
    "Simaneously",
    "Simneously",
    "Simeously",
    "Simously",
    "Simusly",
    "Simsly"
]

# 读取目标文件所有行
with open("Sequences2.txt", 'r') as seq:
    lines = seq.readlines()

# 遍历每个变体进行匹配
for variant in variants:
    match_lines = []
    occurrence_count = 0
    for line in lines:
        # 清理行内的**标记并去除首尾换行符,保证输出和预期一致
        cleaned_line = line.strip().replace('**', '')
        if variant in cleaned_line:
            match_lines.append(cleaned_line)
            occurrence_count += 1
    # 仅当有匹配结果时输出
    if occurrence_count > 0:
        print(variant)
        for matched_line in match_lines:
            print(matched_line)
        print(f"Occurrence of {variant} is: {occurrence_count}\n")

修正说明

  • 直接使用用户指定的变体列表,避免生成逻辑错误导致的偏差。
  • 每个变体单独初始化计数和匹配行列表,确保计数仅针对当前变体。
  • 先收集所有匹配行,再按「变体名称 → 所有匹配行 → 计数」的顺序输出,符合预期格式。
  • 清理原文本中的**标记,保证输出内容和示例一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 01:30:31