[Tutor] Please take a look at this function
Richard D. Moores
rdmoores at gmail.com
Fri Dec 18 05:34:54 CET 2009
On Thu, Dec 17, 2009 at 19:49, Hugo Arts <hugo.yoshi at gmail.com> wrote:
> Probably easiest to use a regular expression to fix that particular
> thing, as in:
>
> import re
> mult_space = re.compile(r'\s+')
> def prestrings2list(a_str):
> return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')]
>
> Hugo
That's perfect, Hugo!
========================================
a_str = "blender , synthetic DNA, myrmecology, fungi, quorum sensing,
theoretical physic's, reason, love, hope, virtual reality, google
operating system, space, life, mystery, truth's, universe, immortality,
strangeness, fun ,living, hope, eternity, knowledge, Egyptian secrets of the
dead, n-space, hyper-time , theory of everything, light, nuclear theory,
particle theory, myrmec, self replicating RNA, MMOG, MMOR%PG,
symbiosis,Black's Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7,
Leibnitz, myrmecology"
def prestrings2list(a_str):
import re
mult_space = re.compile(r'\s+')
return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')]
lst = prestrings2list(a_str)
print(lst)
=================================================
OUTPUT
['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing',
"theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google
operating system', 'space', 'life', 'mystery', "truth's", 'universe',
'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity',
'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time',
'theory of everything', 'light', 'nuclear theory', 'particle theory',
'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's
Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7',
'Leibnitz', 'myrmecology']
Now for a better function name, and a proper docstring. Anyone?
Dick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091217/457ccab2/attachment-0001.htm>
More information about the Tutor
mailing list