I have a huge str ~ 1 GB in length:
>>> len(L) 1073741824
I need to take many fragments of a string from specific indices to the end of the string. In C, I would do:
char* L = ...; char* p1 = L + start1; char* p2 = L + start2; ...
But in Python, line slicing creates a new str instance, using more memory:
>>> id(L) 140613333131280 >>> p1 = L[10:] >>> id(p1) 140612259385360
To save memory, how do I create a str-like object that is actually a pointer to the original L?
Edit : we have buffer and memoryview in Python 2 and Python 3, but memoryview does not show the same interface as str or bytes :
>>> L = b"0" * 1000 >>> a = memoryview(L) >>> b = memoryview(L) >>> a < b Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: memoryview() < memoryview() >>> type(b'') <class 'bytes'> >>> b'' < b'' False >>> b'0' < b'1' True
source share