Python Forum

Full Version: Split string at specific number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there,
at first, excuse my bad English. It's not my mother tongue.

Now to my question:
Through some experiments I got some random strings containing only numbers looking like that:
"02587304986710134775623874160348756018734569238475610876..."
Now i wanna go through the string and split it at each possible alphabetic number.
Because it contains 0, i wanna use the alphabet from 0-25.
This means, that the first numbers i'll get in an array would be the following:
"0, 25, 8, 7, 3, 0, 4, 9, 8, 6, 7, 10, 13, 4, 7, 7, 5, 6, 23, 8, 7, 4, 16, 0, ..."

How could i realize that?

Hope you'll understand what I mean and want to do.
Yours Sincerely,
TimeMen
What have you tried?
Uh, yes.
Here it is.

for i in range(0, len(pi)):
	x = int(pi[i])
	if  0 < x < 3:
		y = int(pi[i+1])
		if x == 1:
			char = int(pi[i] + pi[i+1]) + 65
			chars.append(chr(char))
		if x == 2 and y < 6:
			char = int(pi[i] + pi[i+1]) + 65
			chars.append(chr(char))
print(chars)
The problem is, that it iterates through the number, in this case Pi, but i can't jump twice if there is a number with two decimals which is contained in the alphabet.
Like 14. I can't jump twice to 15, so 4 is also counted as a character, but that not the goal.
Just considered whether a while loop could help me, because I can set the steps each round manually.

What do you think? Or is there any other more effective solution?

Yours Sincerely,
TimeMen
A while loop makes the most sense, unless you're advanced (or enthused) enough to use generators (and even then, the generator might just abstract away the while loop from primary logic, but I haven't thought about this thoroughly).
how you make the difference between
0, 25 ... (see the example) and
0, 2, 5?
both are valid in my opinion
It is a funny problem. If I have understood well when you find a 1 or 2 you need to delay adding them to the list of valid solutions until you can check the next digit and if combining it with the next the number is between 10 and 25 add this instead of both separately. One consequence is that you will never found a 'B' in your output... as they are always stored as 1X numbers.

A Trick to do this is just to keep track of the previous value and iterate char by char. This also makes it easy to work with "unlimited" data sources like a stream:
inp = "02587304986710134775623874160348756018734569238475610876"

valid = []
previous = 0
for c in inp:
    n = int(c)

    if previous == 1:
        # Any number between 10 and 19 is inserted
        # A consequence of this is that it is impossible to have a single 1 in the result.
        n += 10
    
    elif previous == 2:
        # Only numbers between 20 and 25 are considered
        if n <= 5:
            n += 20
        else:
            valid.append(2)
    else:
        # Nothing to do in case previous == 0
        pass

    if n in (1, 2):
        # Wait until the next step to add this number
        previous = n
    else:
        # Just insert it
        valid.append(n)
        previous = 0

# Maybe the last char was 1 or 2...
if previous != 0:
    valid.append(previous)

a = ord('A')
print([chr(a + x) for x in valid])
Output:
['A', 'Z', 'I', 'H', 'D', 'A', 'E', 'J', 'I', 'G', 'H', 'K', 'N', 'E', 'H', 'H', 'F', 'G', 'X', 'I', 'H', 'E', 'Q', 'A', 'D', 'E', 'I', 'H', 'F', 'G', 'A', 'S', 'H', 'D', 'E', 'F', 'G', 'J', 'X', 'I', 'E', 'H', 'F', 'G', 'K', 'I', 'H', 'G']
Some small comments to your code:
- In python if you need to iterate a string or a list with "for i in range(len(pi)):" it is often a sign that there is a better way to do it...
- I preferred to store the numbers and convert them to char at the end, but that is just personal, but try to avoid the magic numbers (65 = ord('A')) as not everyone knows the ASCII table...
How do you decide if the number you get is '10' not '1' and '0'? Or '2' and '5' instead of '25'?