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

在嵌套子列表组中按列提取最大值(禁用Pandas/Numpy)

Hey there! Let's figure out how to solve this nested list max-value problem without using pandas or numpy.

First, let's recap the problem to make sure we're on the same page:
We have a nested list like this:

L = [[[4.0, 4.0], [1.0, 2.0, 3.0]], [[4.0, 5.0], [1.0]]]

We need to extract the maximum value from each "column" of nested sublists. The first column includes [4.0, 4.0] and [4.0, 5.0] (max is 5.0), the second column includes [1.0, 2.0, 3.0] and [1.0] (max is 3.0), so the final result should be [5.0, 3.0].

Solution Code

Here's a straightforward approach that works regardless of how many outer sublists or nested elements you have:

L = [[[4.0, 4.0], [1.0, 2.0, 3.0]], [[4.0, 5.0], [1.0]]]

result = []
# Transpose the outer list to get each "column" of sublists
for column in zip(*L):
    # Collect all elements from every sublist in the current column
    all_elements = []
    for sublist in column:
        all_elements.extend(sublist)
    # Add the maximum value of the column to the result
    result.append(max(all_elements))

print(result)  # Output: [5.0, 3.0]

How It Works

Let's break down the logic step by step:

  1. Transpose the outer list with zip(*L): This is the key trick. It takes your outer list and groups together the sublists that are in the same "column" position. For your example, zip(*L) gives us two tuples:
    • First tuple: ([4.0, 4.0], [4.0, 5.0]) (the first column)
    • Second tuple: ([1.0, 2.0, 3.0], [1.0]) (the second column)
  2. Flatten each column's sublists: For each column, we iterate through every nested sublist and use extend() to add all their elements to a single flat list. This lets us easily find the maximum value across all elements in the column.
  3. Compute and collect max values: Use Python's built-in max() function on the flattened list of column elements, then add that value to our result list.

This approach handles variable numbers of outer sublists, nested sublists, and element counts—no hardcoded indices needed!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:32:43