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

Python中如何提取嵌套数组内各子数组首个对象的面积数据?

解决方案

基础循环实现

直接通过for循环遍历MainArray的每个子数组,提取首个对象的面积:

# 存储提取出的面积数据
first_object_areas = []

# 遍历每个图像对应的子数组
for subarray in MainArray:
    # 假设面积是object数组的第0个元素,根据实际结构调整索引
    area = subarray[0][0]
    first_object_areas.append(area)

简洁的列表推导式(本质也是循环)

如果追求代码简洁,可以用列表推导式一行完成提取:

# 提取所有子数组首个对象的面积(同样根据实际索引调整)
first_object_areas = [subarray[0][0] for subarray in MainArray]

注意事项

  • 上述代码中subarray[0][0]的第二个索引需要根据你的object数组结构调整:比如如果面积是object的第3个数据项,就改成subarray[0][2](Python索引从0开始)。
  • 提取完成后,你可以用绘图库(如matplotlib)可视化数据,示例:
import matplotlib.pyplot as plt

plt.plot(range(len(first_object_areas)), first_object_areas)
plt.xlabel('图像序列序号')
plt.ylabel('首个对象面积')
plt.title('首个对象面积随图像序列变化')
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 03:07:36