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

如何实现基于triggerlist与citylist的文件内容组合匹配逻辑?

Solution for Matching Trigger + City Terms in a File

Got it, let's get this working for you. The core logic we need is: first confirm any term from triggerlist exists in the target file, then check if any term from citylist also exists—only if both conditions are met, output "success".

I'll share two practical approaches: a flexible Python script and a quick shell command, depending on your workflow.


Approach 1: Python Script (Great for Customization)

This is easy to tweak for edge cases like case insensitivity or exact word matches.

def verify_terms(target_file, trigger_list, city_list):
    # Read the entire file content once for efficiency
    with open(target_file, 'r', encoding='utf-8') as f:
        file_content = f.read()
    
    # Step 1: Check if any trigger term exists in the file
    has_trigger = any(trigger in file_content for trigger in trigger_list)
    if not has_trigger:
        return False
    
    # Step 2: If trigger is found, check for city terms
    has_city = any(city in file_content for city in city_list)
    return has_city

# Example setup
triggerlist = ["alert", "warning", "notification"]
citylist = ["New York", "London", "Tokyo"]
target_file = "your_target_file.txt"

# Run the check and output result
if verify_terms(target_file, triggerlist, citylist):
    print("success")
else:
    print("failure")

Quick Tweaks for Edge Cases:

  • Case Insensitivity: Modify the checks to ignore uppercase/lowercase:
    has_trigger = any(trigger.lower() in file_content.lower() for trigger in trigger_list)
    
  • Exact Word Matches: Avoid partial matches (e.g., don't count "alerting" as "alert") using regex word boundaries:
    import re
    has_trigger = any(re.search(rf'\b{re.escape(trigger)}\b', file_content) for trigger in trigger_list)
    

Approach 2: Shell Command (Grep, Fast for Terminal Workflows)

If you prefer using terminal tools, this one-liner gets the job done:

if grep -q -F -f triggerlist.txt target_file.txt && grep -q -F -f citylist.txt target_file.txt; then echo "success"; else echo "failure"; fi

Flag Breakdown:

  • -q: Quiet mode (no output, only returns an exit code to signal match success)
  • -F: Treats patterns as fixed strings (avoids issues with special characters in terms)
  • -f: Reads terms from a file (put one term per line in triggerlist.txt and citylist.txt)

Why Your Previous Attempts Might Have Failed:

  • You might have been checking for both terms on the same line instead of anywhere in the entire file
  • Case sensitivity could have caused misses (e.g., "Alert" vs "alert")
  • Partial matches were being counted when you needed exact words, or vice versa

Let me know if you need adjustments for specific edge cases!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 06:57:47