Essentially I want to suck a line of text from a file, assign the characters to a list, and create a list of all the separate characters in a list -- a list of lists.
At the moment, I've tried this:
fO = open(filename, 'rU') fL = fO.readlines()
That's all I've got. I don't quite know how to extract the single characters and assign them to a new list.
The line I get from the file will be something like:
fL = 'FHFF HHXH XXXX HFHX'
I want to turn it into this list, with each single character on its own:
['F', 'H', 'F', 'F', 'H', ...]
Use the list() class to split a string into a list of characters, e.g. my_list = list(my_str) . The list() class will convert the string into a list of characters.
To do this we use the split() method in string. The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string.
In Python you can split a string with the split() method. It breaks up a string (based on the given separator) and returns a list of strings. To split a string, we use the method . split() .
You can do this using list:
new_list = list(fL)
Be aware that any spaces in the line will be included in this list, to the best of my knowledge.
I'm a bit late it seems to be, but...
a='hello' print list(a) # ['h','e','l','l', 'o']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With