I have a huge str
of ~1GB in length:
>>> len(L)
1073741824
I need to take many pieces of the string from specific indexes until the end of the string. In C I'd do:
char* L = ...;
char* p1 = L + start1;
char* p2 = L + start2;
...
But in Python, slicing a string creates a new str
instance using more memory:
>>> id(L)
140613333131280
>>> p1 = L[10:]
>>> id(p1)
140612259385360
To save memory, how do I create an str-like object that is in fact a pointer to the original L?
Edit: we have buffer
and memoryview
in Python 2 and Python 3, but memoryview
does not exhibit the same interface as an 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