Search code examples
pythonparsingstring-parsing

How do I excecute a command for every split?


I am new to python, and am trying to filter a string that looks similar to this:

"{Red,Plant,Eel}{Blue,Animal,Maple}{Yellow,Plant,Crab}"

And so on for 100s of three word sets.

I want to extract the second word from every set marked by "{ }", so in this example I want the output:

"Plant,Animal,Plant"

And so on.

How can I do it efficiently?

As of Right now I am using string.split(",")[1] individually for each "{ }" group.

Thanks.


Solution

  • This does the trick:

    str_ = "{Red,Plant,Eel}{Blue,Animal,Maple}{Yellow,Plant,Crab}"
    res = [x.split(',')[1] for x in str_[1:-1].split('}{')]
    

    and produces

    ['Plant', 'Animal', 'Plant']
    

    with the str_[1:-1] we remove the initial "{" and trailing "}" and we then split the remaining entities on every instance of "}{" thus producing:

    ["Red,Plant,Eel", "Blue,Animal,Maple", ...]
    

    finally, for every string, we split on "," to obtain

    [["Red", "Plant", "Eel"], ...]
    

    from which we keep only the first element of each sublist with x[1].

    Note that for your specific purpose, slicing the original string with str_[1:-1] is not mandatory (works without it as well), but if you wanted only the first instead of the second item it would make a difference. The same holds in case you wanted the 3rd.


    If you want to concatenate the strings of the output to match your desired result, you can simply pass the resulting list to .join as follows:

    out = ','.join(res)
    

    which then gives you

    "Plant,Animal,Plant"