views:

93

answers:

3

Hello, I have a list of strings that all follow a format of parts of the name divided by underscores. Here is the format:

string="somethingX_somethingY_one_two"

What I want to know how to do it extract "one_two" from each string in the list and rebuild the list so that each entry only has "somethingX_somethingY". I know that in C, there is a strtok function that is useful for splitting into tokens, but I'm not sure if there is a method like that or a strategy to get that same effect in Python. Help me please?

+2  A: 

I think this does what you're asking for.

s = "somethingX_somethingY_one_two"
splitted = s.split( "_" )
splitted = [ x for x in splitted if "something" in x ]
print "_".join( splitted )
robert
+5  A: 

You can use split and a list comprehension:

l = ['_'.join(s.split('_')[:2]) for s in l]
Mark Byers
@killown: you have applied this to the string, instead of to a list of strings. I think I saw a couple other places where you looked back over questions and claimed they didn't work -- maybe try reading the questions a bit more thoroughly first =)?
katrielalex
@katrielalex: it fails if the string is "somethingX_one_two_somethingY"
killown
@killown: It also fails if the string is `"Elephants."` You can't assume you know the problem; you need to use the specification that the OP gave.
katrielalex
yes, i am using it: "What I want to know how to do it extract "one_two from each string"
killown
+3  A: 

If you're literally trying to remove "_one_two" from the end of the strings, then you can do this:

tail_len = len("_one_two")
strs = [s[:-tail_len] for s in strs]

If you want to remove the last two underscore-separated components, then you can do this:

strs = ["_".join(s.split("_")[:-2]) for s in strs]

If neither of these is what you want, then let update the question with more details.

Ned Batchelder
your code fails if string is "somethingX_one_two_somethingY"
killown
@killown: you are right. I gave two interpretations of the under-specified problems. I'm not willing to make many more guesses what the OP wanted.
Ned Batchelder