I have the following string:
foo = 'F9B2Z1F8B30Z4'
I split this string on upper-case F
, as such:
chunks = ['F' elem for elem in foo.split('F') if elem != '']
This gives me:
chunks
['F9B2Z1', 'F8B30Z4']
But, now assume that one of the F
characters is lowercase:
'F9B2Z1f8B30Z4'
Now, we get:
chunks = ['F' elem for elem in foo.split('F') if elem != '']
chunks
['F9B2Z1f8B30Z4']
I'd like to be able to also split on lower-case f
, such as:
chunks = ['F' elem for elem in foo.split('F') if elem != '' <some other condition here>]
chunks
['F9B2Z1' 'f8B30Z4']
I'm somewhat hesitant to use .replace()
because after doing the split I need to perform some tests on the resulting list of chunks (e.g. test whether the first character of each chunk is uppercase or lowercase... and it would be difficult to do that if I replace lowercase Fs with uppercase Fs).
What's the best way to split on uppercase or lowercase without using RegEx?
Thanks!
CodePudding user response:
One way using enumerate
:
foo = 'F9B2Z1f8B30Z4'
indices = [n for n, i in enumerate(foo) if i.lower() == "f"]
[foo[i:j] for i, j in zip(indices, indices[1:] [None])]
Output:
['F9B2Z1', 'f8B30Z4']