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

如何从曲面列表中筛选出对应面积仅重复两次的曲面?

问题需求

给定曲面列表Surface和对应的面积列表S_Area,要求从原曲面列表中仅保留对应面积恰好出现两次的曲面(每个符合条件的面积仅保留一个对应曲面)。

输入数据

Surface = [S1, S2, S3, S4, S5, S6, S7, S8, S9, S10]
S_Area = [35.67, 0.08, 0.08, 39.56, 39.56, 0.08, 9.05, 44.72, 45.36, 45.36]

期望输出

S = [S4, S9]
解决思路
  1. 统计每个面积值在S_Area中的出现次数;
  2. 筛选出出现次数恰好为2的面积值集合;
  3. 遍历原曲面与面积的配对,首次遇到属于该集合的面积时,将对应曲面加入结果,并从集合中移除该面积,确保每个符合条件的面积仅保留一个曲面。
Python 实现代码
from collections import Counter

# 输入数据(假设S1~S10已定义)
Surface = [S1, S2, S3, S4, S5, S6, S7, S8, S9, S10]
S_Area = [35.67, 0.08, 0.08, 39.56, 39.56, 0.08, 9.05, 44.72, 45.36, 45.36]

# 统计面积出现次数
area_count = Counter(S_Area)
# 筛选出恰好出现2次的面积
target_areas = {area for area, cnt in area_count.items() if cnt == 2}

result = []
for surf, area in zip(Surface, S_Area):
    if area in target_areas:
        result.append(surf)
        target_areas.remove(area)  # 移除已处理的面积,避免重复添加

print(f"S = {result}")
结果验证

运行上述代码后,输出结果与期望一致:S = [S4, S9]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 07:10:29