[Tutor] Please take a look at this function
Dave Angel
davea at ieee.org
Fri Dec 18 04:40:38 CET 2009
Richard D. Moores wrote:
> def prestrings2list(a_str):
> word = ""
> list_of_strings = []
> length_of_a_string = len(a_str)
> for i, char in enumerate(a_str):
> if i == length_of_a_string - 1:
> word += char
> word = word.rstrip()
> list_of_strings.append(word)
> elif char == ",":
> word = word.strip()
> list_of_strings.append(word)
> word = ""
> elif char != " ":
> word += char
> elif char == " " and word != "" and a_str[i - 1] != " ":
> word += char
> return list_of_strings
>
>
> The idea for this came from looking at the interests of bloggers on a
> blogging site. Some have a great many interests listed -- so many that I
> thought it would handy to have a function that could put them in a Python
> list so they could be counted, sorted, dupes weeded out, etc.
>
> For example, here's a shorter one that I copied and pasted, modified, and
> then pasted again into a pair of quotes, thereby creating one long Python
> string:
>
> 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"
>
> prestrings2list(a_str) returns
>
> ['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']
>
> This is exactly what I wanted, but please tell me if the function name makes
> any sense, and if the function could be made to conform better to standard
> Python practice. And what a good docstring for it might be.
>
>
> I've assumed that the items in these strings will be separated by commas.
> But there could be some using semicolons instead. That revision will be easy
> to make.
>
>
> Thanks, Tutors,
>
>
> Dick Moores
>
>
Seems to me the function would be much simpler using the string method
.split() Something like:
def myfunc(a_str):
list_of_strings = a_str.split(",")
return list_of_strings
Now, if you want to also allow semicolons, you could pre-translate any
semicolons to commas (using replace()). And if you need to strip
leading and trailing whitespace, you could do a separate pass over the list.
DaveA
More information about the Tutor
mailing list