Python 3 使用split分割输入时触发MemoryError问题咨询
input().split() in Python Hey there, let's break down why you're hitting that MemoryError when running n_list = n_list.split() after n_list = input(), and how to fix it.
What's Causing the Issue?
The core problem here is that input() reads an entire line of input into a single string stored in memory. If that input line is extremely large—think gigabytes of unbroken text, or a single line with millions of space-separated elements—storing that full string already eats up massive memory. When you call .split(), Python then tries to create a list containing every split element, which doubles down on memory usage and pushes your system past its available limit.
Solutions to Try
1. Stream Input Line-by-Line with sys.stdin
Instead of loading the entire input at once, use sys.stdin to process input line by line. This way, you only keep one line in memory at a time:
import sys for line in sys.stdin: # Split the current line and process elements immediately for item in line.split(): # Do your processing here (e.g., convert to int, write to a file, etc.) process_item(item)
This avoids holding the entire input or the full split list in memory.
2. Process Input in Chunks for Ultra-Large Lines
If your input is a single massive line (no newlines), you can read it in small chunks and split incrementally using a generator. This lets you yield split elements one at a time without storing all of them:
import sys def stream_split_input(sep=(' ', '\t', '\n')): buffer = [] # Read input in 4KB chunks (adjust size based on your system's needs) for chunk in iter(lambda: sys.stdin.read(4096), ''): for char in chunk: if char in sep: if buffer: yield ''.join(buffer) buffer = [] else: buffer.append(char) # Yield the last remaining element in the buffer if buffer: yield ''.join(buffer) # Use the generator to process elements one by one for item in stream_split_input(): process_item(item)
3. Verify Your Input Source
Sometimes the issue isn't just size—you might be accidentally reading binary data as text (which creates huge, unreadable strings) or the input stream isn't closing properly, causing input() to hang and accumulate memory. Double-check that your input is formatted correctly (e.g., proper line breaks, expected separators) and that you're reading from the intended source.
Key Takeaway
This MemoryError is all about memory overload. The fix boils down to avoiding loading all input data into memory at once. Use streaming or chunked processing to handle elements incrementally instead of storing everything in a single list.
内容的提问来源于stack exchange,提问作者Hasan Khan

