<span style="font-family: courier new,monospace;">def prestrings2list(a_str):<br> word = ""<br> list_of_strings = []<br> length_of_a_string = len(a_str)<br> for i, char in enumerate(a_str):<br> if i == length_of_a_string - 1:<br>
word += char<br> word = word.rstrip()<br> list_of_strings.append(word)<br> elif char == ",":<br> word = word.strip() <br> list_of_strings.append(word)<br>
word = ""<br> elif char != " ":<br> word += char<br> elif char == " " and word != "" and a_str[i - 1] != " ":<br> word += char<br>
return list_of_strings<br></span><br><br>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. <br>
<br>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:<br><br><span style="font-family: courier new,monospace;">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"</span><br>
<br><span style="font-family: courier new,monospace;">prestrings2list(a_str) returns <br><br>['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']<br>
<br><span style="font-family: verdana,sans-serif;">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.<br>
<br><br>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. <br><br><br>Thanks, Tutors,<br><br><br>Dick Moores</span> <br>
<br></span>