[Tutor] Re: Punctuation
Derrick 'dman' Hudson
dman@dman.ddts.net
Mon Dec 2 08:31:21 2002
--3V7upXqbjpZ4EhLz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On Mon, Dec 02, 2002 at 10:57:51AM -0200, Diego Prestes wrote:
| Im trying to make a program that remove the punctuations of a txt
| file or string because I want to do a count of words later. But when
| a make my program it give Out of memory error. Someone know a
| better way to do this program?
| from string import *
| text =3D raw_input("Text :")
| d1 =3D punctuation
| d =3D split(join(d," "))
| for x in range(len(d)):
| n =3D find(text,d[x])
| text =3D text[:n] + text[n+1:]
| print text
What's happening is you're creating lots of copies of strings.
(namely where you add them together) You end up with almost 4 copies
of the string in memory at one time before 3 of them are freed. If
the input is long, that will use a lot of memory.
How about this:
import string
text =3D raw_input("Text :")
# for each punctuation character
for ch in string.punctuation :
# replace it with the empty string
text =3D text.replace( ch , '' )
print text
Or even :
import string , re
# the same thing as the loop above but using a regular expression
pattern =3D re.compile( "[" + re.escape( string.punctuation ) + "]" )
text =3D raw_input("Text :")
text =3D pattern.sub( "" , text )
print text
-D
--=20
How to shoot yourself in the foot with Java:
=20
You find that Microsoft and Sun have released incompatible class
libraries both implementing Gun objects. You then find that although
there are plenty of feet objects implemented in the past in many other
languages, you cannot get access to one. But seeing as Java is so cool,
you don't care and go around shooting anything else you can find.
(written by Mark Hammond)
=20
http://dman.ddts.net/~dman/
--3V7upXqbjpZ4EhLz
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iEYEARECAAYFAj3rZAoACgkQO8l8XBKTpRT+RwCfVEy9SMzcrEojydt2wXxPXZaC
LqYAoJ6UoUFgMGMIoDQs/8PVd0v2CXSJ
=0QR7
-----END PGP SIGNATURE-----
--3V7upXqbjpZ4EhLz--