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

Python中创建数量不确定的数组时遇numpy索引越界及内存超限问题求助

Fixing IndexError & Memory Issues with Dynamic Array Generation

Hey there! Let's break down what's causing your errors and fix them properly.

The Root of Your Problems

Your current code uses a fixed-size numpy array (a = np.zeros((1000, len(wi)))) to store all generated rows. Here's why this fails:

  • When n is large, you end up generating more than 1000 rows. Since numpy arrays can't grow dynamically, trying to access row 1000 (which is out of bounds—indices start at 0, so max is 999) triggers the IndexError.
  • Making the array absurdly large (like 1e9 rows) is impossible because it requires way more memory than your computer can handle—hence the ValueError about the array being too big.

The Solution: Use Dynamic Lists Instead

Python's built-in lists are perfect for this scenario because they grow dynamically. Instead of pre-allocating a huge numpy array, we'll store each generated row as a new list element, adding rows on the fly with append().

Modified Code with Explanations

Here's your code updated to use dynamic lists, plus fixes for a few other logical issues:

def calculate_sum(i, j, wi, a):
    # Simplified sum calculation using built-in sum() for readability
    return sum(a[j][z] * wi[z] for z in range(i))

# Original input values
rb = [125, 120, 81, 70, 60, 52, 48, 30, 28, 22, 18]
Ru = 645
n = len(rb)
wi = rb
import math

# Initialize empty list to store rows (dynamic, no fixed size!)
a = []

# Generate first row
first_row = [0] * n
first_row[0] = math.floor(Ru / wi[0])
for i in range(1, n):
    remaining = Ru - calculate_sum(i, 0, wi, a)
    first_row[i] = math.floor(remaining / wi[i])
a.append(first_row)

j = 0  # Now using 0-based index for the list
k = n - 2  # Start from second-last element

while k >= 0:
    # Only process if current row's k-th element is greater than 0
    while a[j][k] > 0:
        # Create new row based on previous row
        new_row = a[j].copy()
        new_row[k] -= 1
        # Recalculate elements after k
        for i in range(k + 1, n):
            remaining = Ru - calculate_sum(i, len(a), wi, a)
            new_row[i] = math.floor(remaining / wi[i])
        a.append(new_row)
        j += 1
    k -= 1  # Move to the previous element (fixed your original dead loop here!)

# Print all generated rows
for row in a:
    print(row)

Key Changes Made:

  1. Dynamic Row Storage: Replaced the fixed numpy array with a Python list a. We use append() to add new rows as needed—no more pre-allocation, no more index bounds issues.
  2. Simplified Sum Calculation: Renamed a1 to calculate_sum and used Python's built-in sum() with a generator expression for cleaner, more readable code.
  3. Fixed 0-Based Indexing: Switched to 0-based indexing for the list (since Python lists start at 0), which eliminates confusion with your original 1-based j variable.
  4. Fixed Dead Loop: Your original code reset k = n - 2 inside the loop, which would have caused an infinite loop. We now decrement k properly with k -= 1.
  5. Memory Efficiency: Only stores the rows you actually generate, so you won't waste memory on empty pre-allocated rows.

Why This Works

  • Lists grow dynamically: You can add as many rows as needed without worrying about pre-defining a maximum size.
  • No memory waste: You only use memory for the rows you generate, not for thousands of empty rows.
  • Avoids index errors: Since we're appending rows, we never try to access an index that doesn't exist.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 17:02:29