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

多匹配行场景下Python列表过滤循环失效问题求助

问题描述

编写了一段Python代码,用于读取两个文件内容并分别存入列表,随后创建新列表过滤掉两个列表中的匹配项。当两个文件各有1行匹配内容时,过滤逻辑正常:原列表长度为1,过滤后列表长度为0。但当两个文件各有2行及以上匹配内容时,过滤逻辑完全失效,原列表长度为2,过滤后列表长度仍为2(预期应为0)。

测试案例

单匹配行场景

文件1内容:

loaded_sound,sfx/levels/zombie/maps/asylum/switch/gen_arc/gen_arc_loop.wav
文件2内容与文件1完全一致,过滤结果符合预期。

多匹配行场景

文件1内容:

loaded_sound,sfx/levels/nazi_zombie_sumpf/amb_fire/small/small_00.wav
loaded_sound,sfx/levels/nazi_zombie_sumpf/amb_fire/small/small_01.wav
文件2内容与文件1完全一致,过滤结果不符合预期。

原代码片段

f1 = join(f"{WAW_ROOT_DIR}/zone_source/english/assetlist/{CURRENT_SELECTED_MOD}.csv")
with open(f1, 'r') as assetList:
    assetList_text = [i.strip() for i in assetList]

# this loaded_sounds list will have 2 lines in it.
loaded_sounds = [i.strip() for i in assetList_text if i.startswith("loaded_sound")]
# length = 2
logging.info(f"old loaded sounds: {len(loaded_sounds)}")

path_ = join(f"{WAW_ROOT_DIR}/zone_source/english/assetlist/{CURRENT_SELECTED_MOD}_ignore_sound.csv")
if exists(path_) and isfile(path_):
    with open(path_, 'r') as file:
        # this list also has 2 lines in it (2 matching lines as the list above)
        ignored_sounds_file = [i.strip() for i in file]

    # now what im doing here is creating a new list where i only add items from the 1st list that arent in the 2nd list.
    # this works when both files have 1 matching line.
    # but when i have 2 or more matching lines it fails to do its job and adds both items from 1st list to 2nd list when it shouldnt be adding any at all as theyre matching lines.

    loadedSounds = []
    for i in ignored_sounds_file:
        if i in loaded_sounds:
            for j in loaded_sounds:
                if j != i and j not in loadedSounds:
                    loadedSounds.append(j)
    print("end")

    logging.info(f"new loaded sounds: {len(loadedSounds)}")
else:
    print("no ignore file detected")
问题分析

你的过滤逻辑完全写反了。当前代码的逻辑是:遍历忽略列表中的每一项,若该项存在于待过滤列表中,就把待过滤列表中不等于当前项的元素都加入结果列表。

以多匹配场景为例:

  1. 第一次循环i是small_00.wav那行,此时会把small_01.wav加入loadedSounds
  2. 第二次循环i是small_01.wav那行,此时会把small_00.wav加入loadedSounds
    最终结果列表就包含了所有原本该被过滤掉的元素,完全不符合预期。
修复方案

正确的逻辑应该是:遍历待过滤列表loaded_sounds,只保留那些**不在忽略列表ignored_sounds_file**中的元素。可以用列表推导式快速实现,或者用循环逐个判断。

修复后的代码片段

# 替换原有的loadedSounds循环部分
loadedSounds = [sound for sound in loaded_sounds if sound not in ignored_sounds_file]

# 或者用显式循环(如果需要更复杂的判断逻辑)
loadedSounds = []
for sound in loaded_sounds:
    if sound not in ignored_sounds_file:
        loadedSounds.append(sound)

print("end")
logging.info(f"new loaded sounds: {len(loadedSounds)}")

如果忽略列表元素较多,建议把ignored_sounds_file转换成集合,这样in操作的时间复杂度会从O(n)降到O(1),提升效率:

ignored_sounds_set = set(ignored_sounds_file)
loadedSounds = [sound for sound in loaded_sounds if sound not in ignored_sounds_set]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 08:16:02