-1

I have the following string foo:

foo = 'F9B2Z1F8B30Z4'

Without using re, I'd like to split on 'F' and then add 'F' back to the resulting list.

My attempt is:

['F'+elem for elem in foo.split('F')]

This gives us:

['F', 'F9B2Z1', 'F8B30Z4']

But, I was expecting:

['F9B2Z1', 'F8B30Z4']

Is it possible to modify the list comprehension to catch this case? If not, is there another approach that I can use?

Thanks!

equanimity
  • 2,371
  • 3
  • 29
  • 53
  • `[f"F{elem}" for elem in foo.split("F") if elem]` – ddejohn Feb 08 '22 at 03:38
  • 2
    "Without using re" - why? – user2357112 Feb 08 '22 at 03:40
  • 1
    https://stackoverflow.com/questions/7866128/python-split-without-removing-the-delimiter does not answer my question. The suggested answer fails in certain cases. – equanimity Feb 08 '22 at 03:41
  • 1
    Fails in what cases? Do you think that might be important information to include in your post? – ddejohn Feb 08 '22 at 03:42
  • 1
    Did you read all of the comments on that post? (I did) – equanimity Feb 08 '22 at 03:43
  • 1
    Congrats. I'll ask again, fails in what cases? Did you try the "solution" I suggested in my first comment? We cannot help you solve a problem you refuse to describe. – ddejohn Feb 08 '22 at 03:44
  • @ddejohn: The answer there is straight-up wrong. Multiple comments there have provided failure cases. Your comment fails for similar reasons on similar cases. – user2357112 Feb 08 '22 at 03:45
  • Yes, your solution works in my case. You asked if that post answers my question (it does not due to the edge cases that will fail). – equanimity Feb 08 '22 at 03:46
  • 1
    Why don't you provide a sample string with such edge case? – mozway Feb 08 '22 at 03:48
  • @user2357112supportsMonica yes, there are issues with this approach, but so far OP hasn't provided any evidence that it doesn't work for their specific use-case, which is incredibly vague. Not sure why OP is being cagey. Also not sure why regex isn't an option. – ddejohn Feb 08 '22 at 03:48
  • The deficiencies in the accepted answer in https://stackoverflow.com/questions/7866128/python-split-without-removing-the-delimiter could affect me, hence this post. Definitely not being "cagey" or vague. At least not trying to be... – equanimity Feb 08 '22 at 03:51
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that more accurately represents the problem you are solving. – ddejohn Feb 08 '22 at 03:52
  • 1
    @equanimity could you please edit your question to provide a few more sample cases and make this question self sufficient? – mozway Feb 08 '22 at 03:52
  • 1
    So... after all that... you accepted an answer here that 1) uses regex, which you explicitly wanted to avoid, and 2) uses the exact same logic as the accepted answer in the duplicate you said didn't answer your question. – ddejohn Feb 08 '22 at 04:01
  • He appended a *non*-regex answer to his original answer. – equanimity Feb 08 '22 at 04:03
  • Which is exactly the same as what I posted in the very first comment... which is also exactly the same as in that duplicate. – ddejohn Feb 08 '22 at 04:05

2 Answers2

1

Actually your problem might be more amenable to use re.findall:

foo = 'F9B2Z1F8B30Z4'
parts = re.findall(r'F[^\WF]*', foo)
print(parts)  # ['F9B2Z1', 'F8B30Z4']

The regex pattern matches a leading F followed by zero or more word characters, which are not F.

To do this using non regex split, we can slightly modify your list comprehension to only retain non empty string elements from the split:

parts = ['F' + elem for elem in foo.split('F') if elem != '']
print(parts)  # ['F9B2Z1', 'F8B30Z4']
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Without regex we can do it by replacing the F with some other delimiter before it then spiting it at the replaced delimiter and filtering empty strings

[elem for elem in foo.replace("F","-F").split("-") if elem]

THUNDER 07
  • 521
  • 5
  • 21