Split string > list of sublists of words and characters
<p>No imports allowed (it's a school assignment).</p>

<p>Wish to split a random string into a list of sublists. Words in a sublist, all other characters (including whitespace) would be in a sublist containing only one item. Anyone have some advice on how to do this;</p>

<pre><code>part = "Hi! Goodmorning, I'm fine."
list = [[H,i],[!],[_],[G,o,o,d,m,o,r,n,i,n,g],[,],[_],[I],['],[m],[_],[f,i,n,e],[.]]
</code></pre>

<p>This does the trick:</p>

<pre><code>globalList = []
letters = "abcdefghijklmnopqrstuvwxyz"
message = "Hi! Goodmorning, I'm fine."
sublist = []
for char in message:
    #if the character is in the list of letters, append it to the current substring
    if char.lower() in letters:
        sublist.append(char)
    else:
        #add the previous sublist (aka word) to globalList, if it is not empty
        if sublist: 
            globalList.append(sublist)
        #adds the single non-letter character to the globalList
        globalList.append([char])
        #initiates a fresh new sublist
        sublist = []
print(globalList)
#output is [['H', 'i'], ['!'], [' '], ['G', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g'], [','], [' '], ['I'], ["'"], ['m'], [' '], ['f', 'i', 'n', 'e'], ['.']]
</code></pre>

python python-2.7