I have a code at the moment that takes a line from a .txt file and then turns it into a list, with the whitespace stripped.
Looks like this:
fd = open(filename,'rU')
chars = []
for line in fd:
chars.append(line.strip())
return chars
The output looks like this:
['TTTTT', 'TTHHT', 'HHTHT', 'HHHHH', 'THTHT', '']
What I want to do here is turn the out put to look like this:
[['T', 'T', 'T', 'T', 'T'], ['T', 'T', 'H', 'H', 'T',] etc...]
I want to separate them into single elements, but also keep them within their original lists.
I don’t know why you want to do this, as you can do almost anything you can do with a
listwith astr— except change it.Assuming you have a good reason, you probably want to change your loop to
or after the loop,
Will do it. It will run
liston each item inchars.On Python 2, it will make a
listout of the result.On Python 3, you’ll need to do
or
if you want it back as a list-o-lists.