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

Python脚本列匹配索引错误求助:ValueError问题排查

解决Python脚本中的ValueError:列表不在列表中

Hey there, let's break down what's causing that error and fix your script to meet your actual requirement.

为什么会出现ValueError: ['A', 'M'] is not in list?

The list.index() method is designed to take a single element as input, then return the first index where that element appears in the list.

In your code, you're passing match—which is a list (['A', 'M'])—to C2.index(). Python looks through C2 (which contains individual strings or None values) and can't find the list ['A', 'M'] as an element, hence the error.

修正代码以实现你的需求逻辑

Your core goal is:

  1. Find non-empty elements in C2 that exist in C3
  2. For each matching element, check if the corresponding C1 values (from the matching C2 index and matching C3 index) are the same
  3. If they match, display the C4 value from the matching C2 index

Here's a revised script that accomplishes this:

import sys

# Your extracted data (adjusted to align with the Excel rows you provided)
C1 = ['P', 'C', 'P', 'A', 'E', 'E', 'F', 'F', None, None, None]
C2 = [None, None, None, None, 'B', 'A', 'D', 'E', 'M', 'F', 'F']
C3 = ['A', 'X', 'K', 'M', None, None, None, None, None, None, None]
C4 = ['X', 'M', 'P', 'D', 'K', 'F', 'Q', None, None, None, None]

# Step 1: Get non-empty elements in C2 that exist in C3
match_elements = [name for name in C2 if name is not None and name in C3]

if not match_elements:
    print("No matches")
    sys.exit(1)

# Step 2: Process each matching element
for target in match_elements:
    # Get all indices in C2 where the element matches
    c2_matching_indices = [idx for idx, val in enumerate(C2) if val == target]
    # Get all indices in C3 where the element matches
    c3_matching_indices = [idx for idx, val in enumerate(C3) if val == target]
    
    # Step 3: Check corresponding C1 values and output C4 if they match
    for c2_idx in c2_matching_indices:
        for c3_idx in c3_matching_indices:
            # Ensure C1 values aren't None before comparing
            if C1[c2_idx] and C1[c3_idx] and C1[c2_idx] == C1[c3_idx]:
                print(f"Match found! Corresponding C4 value: {C4[c2_idx]}")

Key improvements in this code:

  • Uses enumerate() to find all indices where the target element appears (instead of just the first one, which index() does)
  • Filters out None values upfront in the match_elements list comprehension (no need for a separate filtering step)
  • Explicitly checks that C1 values are non-null before comparing to avoid errors
  • Iterates through all possible index pairs to cover all matching scenarios

Testing with your data

For your provided data:

  • match_elements will be ['A', 'M'] (as you expected)
  • For 'A': it's at index 5 in C2. In C3, 'A' is at index 0. Check C1[5] ('E') vs C1[0] ('P')—they don't match, so no output.
  • For 'M': it's at index 8 in C2. In C3, 'M' is at index 3. Check C1[8] (None) vs C1[3] ('A')—no match, so no output.

If you adjust your data to have matching C1 values, the script will output the corresponding C4 value as intended.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:40:42