memory - Python str view -
i have huge str of ~1gb in length:
>>> len(l) 1073741824 i need take many pieces of string specific indexes until end of string. in c i'd do:
char* l = ...; char* p1 = l + start1; char* p2 = l + start2; ... but in python, slicing string creates new str instance using more memory:
>>> id(l) 140613333131280 >>> p1 = l[10:] >>> id(p1) 140612259385360 to save memory, how create str-like object in fact pointer original l?
edit: have buffer , memoryview in python 2 , python 3, memoryview not exhibit same interface str or bytes:
>>> l = b"0" * 1000 >>> = memoryview(l) >>> b = memoryview(l) >>> < 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
there memoryview type:
>>> v = memoryview('potato') >>> v[2] 't' >>> v[-1] 'o' >>> v[1:4] <memory @ 0x7ff0876fb808> >>> v[1:4].tobytes() 'ota'
Comments
Post a Comment