Python Forum
Split string at specific number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split string at specific number
#1
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
Reply
#2
What have you tried?
Reply
#3
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
Reply
#4
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).
Reply
#5
how you make the difference between
0, 25 ... (see the example) and
0, 2, 5?
both are valid in my opinion
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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...
Reply
#7
How do you decide if the number you get is '10' not '1' and '0'? Or '2' and '5' instead of '25'?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,567 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,165 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
Question Extracting Version Number from a String britesc 2 1,105 May-31-2023, 10:20 AM
Last Post: britesc
  [split] Parse Nested JSON String in Python mmm07 4 1,549 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,311 Jul-11-2022, 08:52 AM
Last Post: japo85
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,308 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Split string knob 2 1,885 Nov-19-2021, 10:27 AM
Last Post: ghoul
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,238 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Remove Specific Columns when the number of columns is greater than a specific value CuriousOne 0 1,326 Sep-09-2021, 09:17 PM
Last Post: CuriousOne

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020