Splitting a string every 'n'
Shagshag13
shagshag13 at yahoo.fr
Tue Jul 9 09:46:17 EDT 2002
<Simon.Foster at smiths-aerospace.com> a écrit dans le message de news: mailman.1026219434.22644.python-list at python.org...
>
> What is the idiomatic way to split a string into a list
> containing 'n' character substrings? I normally do
> something like:
>
> while strng:
> substring = strng[:n]
> strng = strng[n:]
> <process substring>
>
> But the performance of this is hopeless for very long strings!
> Presumable because there's too much list reallocation? Can't Python
> just optimise this by shuffling the start of the list forward?
>
> Any better ideas, short of manually indexing through? Is there
> something like:
>
> for substring in strng.nsplit():
> <process substring>
>
>
(i'm replying but as i'm still a newbie i don't know if it's a good idea - but just trying and hope that gurus will correct)
x = []
i0 = 0
for i in range(n,len(strng) + n,n):
x.append(strng[i0:i])
i0 = i
map(<processing substring function>, x)
s13.
More information about the Python-list
mailing list