Splitting a list of strings
sross at connectmail.carleton.ca
Wed Sep 18 01:59:54 CEST 2002
I need to read a list of strings from a file, weed out the comments and
separate "attribute" descriptions from the data. I have a working
implementation, but I'm interested in finding out if there's a better way to
Here's the code:
"Filters *.arff file into lists of data and attribute strings"
lines = open(filename).readlines()
# All lines beginning with "@'" are attributes, except the last one.
attributes = [str for str in lines if str == "@"]
# Remove attributes from lines, so we don't process them again to find
lines = [str for str in lines if not str in attributes]
attributes = attributes[:-1]
# Filter comments out to get data.
data = [str for str in lines if not str=="%" ]
return (data, attributes)
What I'm interested in knowing is, can I extract all of the "attribute"
strings from lines in such a way that I get my list of "attribute" strings,
but all of those strings have been removed from lines as well, all at the
same time? i.e., without filtering the list a second time. As I've stated,
the code above works, but I'm concerned that it may be sub-optimal.
Thanks, in advance, for your suggestions,
More information about the Python-list