Home > Blockchain >  Is there any way to split string into array by spaces in another string in Python?
Is there any way to split string into array by spaces in another string in Python?

Time:12-16

I have two input strings. In the first one - words with spaces. In the second - the word with same count of symbols without spaces. The task is to split the second string into array by spaces in the first one.

I tried to make it with cycles but there is problem of index out of range and i can't find another solution.

a = str(input())
b = str(input())
b_word = str()
b_array = list()

for i in range(len(a)):
    if a[i] != " ":
        b_word  = b[i]
    else:
        b_array  = b_word
        b_word = str()
print(b_array)

Input:

>>head eat
>>aaabbbb

Output:

Traceback (most recent call last):
  File "main.py", line 29, in <module>
    b_word  = b[i]
IndexError: string index out of range

Expected output:

>> ["aaab", "bbb"]

Thanks in advance!

CodePudding user response:

a = input()  # you don't need to wrap these in str() since in python3 input always returns a string
b = input()
output = list()

for i in a.split(' '):  # split input a by spaces
    output.append(b[:len(i)])  # split input b
    b = b[len(i):]  # update b

print(output)

Output:

['aaab', 'bbb']

CodePudding user response:

You can do something like this:

a = input()
b = input()

splitted_b = []
idx = 0
for word in a.split():
    w_len = len(word)
    splitted_b.append(b[idx:idx w_len])
    idx  = w_len

print(splitted_b)

The idea is taking consecutive sub-strings from b of the length of each word on a.

CodePudding user response:

Instead of using indices, you can iterate over each character of a. If the character is not a space, add the next character of b to your b_word. If it is a space, add b_word to the b_array

b_iter = iter(b) # Create an iterator from b so we can get the next character when needed

b_word = []
b_array = []

for char in a:
    # If char is a space, and b_word isn't empty, append it to the result
    if char == " " and b_word:
        b_array.append("".join(b_word))
        b_word = []
    else:
        b_word.append(next(b_iter)) # Append the next character from b to b_word

if b_word: # If anything left over in b_word, append it to the result
    b_array.append("".join(b_word))

Which gives b_array = ['aaab', 'bbb']

Note that I changed b_word to a list that I .append to every time I add a character. This prevents the entire string from being recreated every time you append a character. Then join all the characters using "".join(b_word) before appending it to b_array.

CodePudding user response:

So to accomodate for any number of spaces in the input it gets a bit more complex as the indexes of the letters will change with each space that is added. So to gather all of the spaces in the string I created this loop which will account of the multiple spaces and alter the index with each new space in the initial word.

indexs = []
new = ''
for i in range(len(a)):
    if len(indexs) > 0:
        if a[i] == ' ':
            indexs.append(i-len(indexs))
    else:
        if a[i] == ' ':
            indexs.append(i)

Then we simple concatenate them together to create a new string that includes spaces at the predetermined indexes.

for i in range(len(b)):
    if i in indexs:
        print(i)
        new  = " "
        new  = b[i]
    else:
        new  = b[i]
print(new)

Hope this helps.

  • Related