How efficient is extracting a Python substring?

I have all the contents of a text file (at least a few kilobytes) in a line myStr.

Will the following code create a copy of the string (less than the first character) in memory?

myStr = myStr[1:]

I hope this just applies to a different location in the same internal buffer. If not, is there a better way to do this?

Thank!

Note. I am using Python 2.5.

+3
source share
4 answers

2.6, ; string_slice() PyString_FromStringAndSize(). , , .

API- ( , , ), .

+4

, , , . , , , .

, . , , , .

, ? , , ? array.array('u')?

+3

One (albeit slightly hacked) solution would be something like this:

f = open("test.c")
f.read(1)
myStr = f.read()
print myStr

It will skip the first character, and then read the data in a string variable.

+1
source

Depending on what you are doing, it itertools.islicemay be the appropriate solution to save memory (if necessary).

+1
source

Source: https://habr.com/ru/post/1737212/


All Articles