Jupyter调用组合函数触发MemoryError但Spyder可正常运行
Hey there! Let's break down why your GetRulesCombinations function works smoothly in Spyder but throws a MemoryError in Jupyter, plus how to fix this issue.
The Root Cause
The problem comes down to excessive memory usage from storing every combination at once. When your selected_list is large, the total number of combinations grows exponentially. For example, a list of 25 elements with start_index=1 and end_index=25 would generate over 33 million combinations—each stored as a separate list in AllCombinations. This quickly eats up all available RAM, triggering the error.
Spyder might not hit this error because:
- Its Python process may have access to more allocated memory than Jupyter's kernel
- Your system had more free RAM available when running the function in Spyder
Solution: Use a Generator to Avoid Full Memory Load
Instead of storing every combination in a list upfront, rewrite your function as a generator that yields combinations one at a time. This way, only one combination exists in memory at any given moment, completely eliminating the MemoryError.
Here's the updated code:
import itertools def GetRulesCombinations(selected_list, start_index, end_index): for i in range(start_index, end_index + 1): for combo in itertools.combinations(selected_list, i): yield list(combo)
How to Use the Generator
Instead of assigning all combinations to a variable like comb = GetRulesCombinations(...), iterate over the generator to process each combination individually:
for combo in GetRulesCombinations(range(5), 1, 3): # Do your processing here (e.g., print, filter, or save to file) print(combo)
If You Need to Store Some Combinations
If you must save a subset of combinations, consider these options:
- Process combinations in small batches and write results to a file instead of keeping them in memory
- Filter combinations on-the-fly (e.g., only keep those that meet specific criteria) to reduce the number of stored items
内容的提问来源于stack exchange,提问作者lrh09

