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

获取含None元素的嵌套列表最小值及其索引

Solution for Finding Minimum Value and Its Indices in Nested List with None Values

Let's walk through how to solve this problem step by step—we'll need to iterate through the nested list, skip None entries, track the smallest numerical value, and record its full (i,j) indices (including all occurrences if there are duplicates).

Approach

  1. Initialize tracking variables: Start with min_value set to a very large number (like float('inf')) so any valid numerical value will be smaller initially. Use an empty list min_positions to store all indices where the minimum occurs.
  2. Loop through the nested list: Use enumerate() to get both the index i of each sublist and the sublist itself. Then loop through each element in the sublist with enumerate() again to get the element's index j.
  3. Skip None values: Only process elements that are not None.
  4. Update minimum and positions:
    • If the current element is smaller than min_value, update min_value and reset min_positions to this single (i,j) pair.
    • If the element equals min_value, add its (i,j) index to min_positions.

Code Example

aList = [[None, 8.0, 1.0], [2.0, 3.0], [9.0], [5.0, None, 4.0]]

min_value = float('inf')
min_positions = []

for i, sublist in enumerate(aList):
    for j, num in enumerate(sublist):
        if num is not None:
            if num < min_value:
                min_value = num
                min_positions = [(i, j)]
            elif num == min_value:
                min_positions.append((i, j))

# Print results
print(f"Minimum value: {min_value}")
print(f"Positions of minimum: {min_positions}")

Output for Your Example

Minimum value: 1.0
Positions of minimum: [(0, 2)]

Edge Cases to Handle

  • Only None values: Add a check after processing to handle this scenario, e.g., raise an error or return a message like "No valid numerical values found."
  • Multiple minima: The code above collects all (i,j) pairs where the minimum occurs. For example, if your list was [[1.0, None], [1.0, 2.0]], the output would be Positions of minimum: [(0, 0), (1, 0)].
  • Empty sublists: The code handles this automatically since enumerate() on an empty list won't run the inner loop.

Simplified Version (First Occurrence Only)

If you only need the first instance of the minimum value (instead of all occurrences), you can simplify the code:

aList = [[None, 8.0, 1.0], [2.0, 3.0], [9.0], [5.0, None, 4.0]]

min_value = float('inf')
min_index = None

for i, sublist in enumerate(aList):
    for j, num in enumerate(sublist):
        if num is not None and num < min_value:
            min_value = num
            min_index = (i, j)

print(f"Minimum value: {min_value}")
print(f"First occurrence position: {min_index}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:34:26