Splitting a list of strings

Sean Ross sross at connectmail.carleton.ca
Wed Sep 18 09:07:11 EDT 2002


Terry:
Thank you. However, I do not decide what format will be applied to the data,
I operate on what is provided. Moreover, the data really is not what I'm
interested in. (Mark asked to see a sample, so I provided it.). Your
suggestion is certainly true and I would follow it, if I could.

What I was interested in was finding a way to do a three pronged filter of a
list of strings using a single list comprehension. Sort of like a change
filter, where you drop handfuls of change into the top of the device and
they come out the other side sorted into pennies, nickels, dimes, quarters.
In this case I had comments, attributes, and data. The comments could be
redirected to a chaff outlet, i.e., ignored, so long as the attributes and
data where filtered properly.
The suggestions people have provided are fine (using an attribute and data
list, marching through the list of lines from the file and sticking an
attribute string into the attribute list and a data string into the data
list). They do what I need to accomplish, but they are not quite what I was
looking for. Think of it this way, if I have a pile of pennies, nickels, and
dimes, and pull out all the nickels, then I have a pile of nickels and a
pile of pennies and dimes. Using list comprehensions, I've been able to
create the pile of nickels, but the original pile is unchanged. That's as it
should be, I suppose. However, I guess what I was looking for was a
side-effect, or some such mechanism where, by extracting the nickels, I get
the two piles: one of nickels, one of pennies and dimes.
I want my cake, and to eat it too, in other words.
You can probably just chalk this all up to an infatuation with list
comprehensions.

Thanks again, to all, for your suggestions
Sean






More information about the Python-list mailing list