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

Python验证生成幂集:仅基于<>内数据校验组合有效性

Alright, let's break this down and build a Python solution to validate those power set combinations from your Excel file. The key here is focusing only on the elements wrapped in <> to check if each combination is valid.

Step 1: Understand the Validation Rules

First, let's define what makes a combination valid based on your sample:

  • All parts not wrapped in <> (like C(A2), C(60), xy1, [dress0], etc.) must be identical across every valid combination — these are fixed and shouldn't change.
  • The elements wrapped in <> (<Pen(x)> and <jack(c)>) can either appear as-is or be replaced with NULL. These form the full power set of the original <> elements (4 total combinations, which matches your sample).

Step 2: Python Implementation

We'll use pandas to read the Excel file, and regex/string checks to validate each entry. First, install the required packages if you haven't:

pip install pandas openpyxl

Here's the full code:

import pandas as pd
import re

def validate_combination(comb_str, fixed_template, valid_options):
    # Split the combination to isolate the variable <>/NULL parts
    # We use the fixed sections as separators
    split_pattern = r'-C\(A2\)-C\(60\)-|-xy1-\[dress0\]-C\(D0\)-lbr-'
    parts = re.split(split_pattern, comb_str)
    
    # If the split doesn't give us exactly 3 parts, the structure is invalid
    if len(parts) != 3:
        return False, "Invalid structure: Doesn't match the required fixed template"
    
    pen_part, jack_part = parts[0], parts[1]
    
    # Check if each variable part is in the allowed options
    if pen_part not in valid_options['pen']:
        return False, f"Invalid pen component: {pen_part} (must be <Pen(x)> or NULL)"
    if jack_part not in valid_options['jack']:
        return False, f"Invalid jack component: {jack_part} (must be <jack(c)> or NULL)"
    
    # Reconstruct the combination from valid parts to ensure fixed sections are unmodified
    reconstructed = fixed_template.format(pen=pen_part, jack=jack_part)
    if reconstructed != comb_str:
        return False, "Fixed sections do not match the required template"
    
    return True, "Valid combination"

# Configure based on your sample data
fixed_template = "{pen}-C(A2)-C(60)-{jack}-xy1-[dress0]-C(D0)-lbr-"
valid_options = {
    'pen': ['<Pen(x)>', 'NULL'],
    'jack': ['<jack(c)>', 'NULL']
}

# Read your Excel file (replace 'your_combinations.xlsx' with your actual file path)
# Assumes combinations are in the first column with no header
df = pd.read_excel('your_combinations.xlsx', header=None, names=['combinations'])

# Run validation on each row
df['is_valid'] = df['combinations'].apply(lambda x: validate_combination(x, fixed_template, valid_options)[0])
df['validation_note'] = df['combinations'].apply(lambda x: validate_combination(x, fixed_template, valid_options)[1])

# Print results to console
print("Validation Results:")
print(df[['combinations', 'is_valid', 'validation_note']])

# Optional: Save results to a new Excel file
df.to_excel('validation_results.xlsx', index=False)

Step 3: How This Works

  • Template Splitting: We use regex to split each combination string at the fixed sections, which lets us pull out only the variable <>/NULL parts without manually parsing every character.
  • Option Validation: We check if each variable part is one of the allowed values (either the original <> element or NULL).
  • Reconstruction Check: We build the combination back from valid parts to make sure the fixed sections haven't been altered (e.g., no typos like C(A3) instead of C(A2)).

Testing with Your Sample Data

If you want to test this code with the sample combinations you provided, just run this snippet:

sample_data = [
    "<Pen(x)>-C(A2)-C(60)-<jack(c)>-xy1-[dress0]-C(D0)-lbr-",
    "<Pen(x)>-C(A2)-C(60)-NULL-xy1-[dress0]-C(D0)-lbr-",
    "NULL-C(A2)-C(60)-<jack(c)>-xy1-[dress0]-C(D0)-lbr-",
    "NULL-C(A2)-C(60)-NULL-xy1-[dress0]-C(D0)-lbr-"
]

test_df = pd.DataFrame(sample_data, columns=['combinations'])
test_df['is_valid'] = test_df['combinations'].apply(lambda x: validate_combination(x, fixed_template, valid_options)[0])
test_df['validation_note'] = test_df['combinations'].apply(lambda x: validate_combination(x, fixed_template, valid_options)[1])

print(test_df)

All four sample combinations will return True with the note "Valid combination" — which is correct, since they're exactly the full power set of your two <> elements.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 11:07:50