Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90745
| References | <6a383ce2-5975-4225-b4f2-f744c9d7a516@googlegroups.com> <mailman.72.1431783967.17265.python-list@python.org> <4272d9d9-3d5b-4b8b-9875-6b66634b490c@googlegroups.com> <measurements-20150516160609@ram.dialup.fu-berlin.de> <f3836ca1-3f87-4c5c-9839-4d8a35aa77e4@googlegroups.com> |
|---|---|
| Date | 2015-05-17 02:59 +1000 |
| Subject | Re: Fastest way to remove the first x characters from a very long string |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.78.1431795548.17265.python-list@python.org> (permalink) |
On Sun, May 17, 2015 at 2:22 AM, <bruceg113355@gmail.com> wrote:
> # Original Approach
> # -----------------
> ss = ss.split("\n")
> ss1 = ""
> for sdata in ss:
> ss1 = ss1 + (sdata[OFFSET:] + "\n")
>
>
> # Chris's Approach
> # ----------------
> lines = ss.split("\n")
> new_text = "\n".join(line[8:] for line in lines)
Ah, yep. This is exactly what str.join() exists for :) Though do make
sure the results are the same for each - there are two noteworthy
differences between these two. Your version has a customizable OFFSET,
where mine is hard-coded; I'm sure you know how to change that part.
The subtler one is that "\n".join(...) won't put a \n after the final
string - your version ends up adding one more newline. If that's
important to you, you'll have to add one explicitly. (I suspect
probably not, though; ss.split("\n") won't expect a final newline, so
you'll get a blank entry in the list if there is one, and then you'll
end up reinstating the newline when that blank gets joined in.) Just
remember to check correctness before performance, and you should be
safe.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fastest way to remove the first x characters from a very long string bruceg113355@gmail.com - 2015-05-16 06:28 -0700
Re: Fastest way to remove the first x characters from a very long string Joel Goldstick <joel.goldstick@gmail.com> - 2015-05-16 09:43 -0400
Re: Fastest way to remove the first x characters from a very long string Chris Angelico <rosuav@gmail.com> - 2015-05-16 23:45 +1000
Re: Fastest way to remove the first x characters from a very long string bruceg113355@gmail.com - 2015-05-16 07:02 -0700
Re: Fastest way to remove the first x characters from a very long string bruceg113355@gmail.com - 2015-05-16 09:22 -0700
Re: Fastest way to remove the first x characters from a very long string Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-16 10:57 -0600
Re: Fastest way to remove the first x characters from a very long string Chris Angelico <rosuav@gmail.com> - 2015-05-17 02:59 +1000
Re: Fastest way to remove the first x characters from a very long string bruceg113355@gmail.com - 2015-05-16 10:35 -0700
Re: Fastest way to remove the first x characters from a very long string Cameron Simpson <cs@zip.com.au> - 2015-05-17 08:41 +1000
Re: Fastest way to remove the first x characters from a very long string Grant Edwards <invalid@invalid.invalid> - 2015-05-16 14:59 +0000
Re: Fastest way to remove the first x characters from a very long string Rustom Mody <rustompmody@gmail.com> - 2015-05-16 08:13 -0700
Re: Fastest way to remove the first x characters from a very long string bruceg113355@gmail.com - 2015-05-16 09:24 -0700
Re: Fastest way to remove the first x characters from a very long string Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-05-16 18:55 +0200
Re: Fastest way to remove the first x characters from a very long string Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-16 23:24 +0000
csiph-web