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

纯Python实现类SQL分组功能,获取指定规则标签

Pure Python Implementation of SQL-like Requirements

Alright, let's break down how to solve this problem with plain Python (no Pandas required). We'll handle both steps clearly and concisely:

Step 1: Find the maximum number of "/" occurrences in the second column

First, we’ll calculate how many times "/" appears in each entry’s second column, then grab the highest count from those values.

Step 2: Retrieve labels where "/" count is one less than the maximum

Next, we’ll filter out entries where the second column’s "/" count equals the max count minus 1, and collect their corresponding first-column labels.

Full Code Example

# Define your input data
data = [
    ['MYDATA_FILE_XT', '/MYDATA/FILE/XT/ROW/STATUS', 'string'],
    ['MYDATA_FILE_XT_ROW', '/MYDATA/FILE/XT/ROW/STATUS', 'string'],
    ['MYDATA_FILE_XT_ROW_STATUS', '/MYDATA/FILE/XT/ROW/STATUS', 'string']
]

# Step 1: Calculate the maximum number of "/" in the second column
slash_counts = [item[1].count('/') for item in data]
max_slash_count = max(slash_counts)
print(f"Maximum '/' occurrences: {max_slash_count}")

# Step 2: Collect labels where "/" count is max_slash_count - 1
target_labels = [item[0] for item in data if item[1].count('/') == max_slash_count - 1]
print(f"Labels meeting the criteria: {target_labels}")

Explanation

  • Step 1: We use a list comprehension to generate a list of "/" counts for every entry’s second column. The max() function then pulls the highest value from that list. For your sample data, this returns 5 (since /MYDATA/FILE/XT/ROW/STATUS contains 5 "/" characters).
  • Step 2: Another list comprehension filters entries by checking if their second column’s "/" count is exactly one less than the maximum. We collect the first-column labels of these matching entries. For your sample data, since all entries share the same second column, there are no matches—so the result is an empty list [].

If you had entries with varying second columns, this code would automatically adjust to find the correct max count and corresponding labels.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:51:49