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
nis 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 theIndexError. - Making the array absurdly large (like 1e9 rows) is impossible because it requires way more memory than your computer can handle—hence the
ValueErrorabout 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:
- Dynamic Row Storage: Replaced the fixed numpy array with a Python list
a. We useappend()to add new rows as needed—no more pre-allocation, no more index bounds issues. - Simplified Sum Calculation: Renamed
a1tocalculate_sumand used Python's built-insum()with a generator expression for cleaner, more readable code. - 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
jvariable. - Fixed Dead Loop: Your original code reset
k = n - 2inside the loop, which would have caused an infinite loop. We now decrementkproperly withk -= 1. - 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
相关产品推荐
相关产品推荐

