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

Question:

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!

Asked By: Dron Adron

||

Answers:

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.

Answered By: Jorge Morgado

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.

Answered By: Pranav Hosangadi
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']
Answered By: mr_mooo_cow

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.

Answered By: Ethan Chartrand

Code


sone = input()                 
stwo = 'zzzzzxxxyyyyy'
nwz = []
wrd = ''
cnt = 0
idx = 0

spc = sone.split(' ')      #split by whitespace
a = [len(i) for i in spc]  #list word lengths w/out ws

for i in stwo:
  if cnt == a[idx]:        #if current iter eq. word length w/out ws
    nwz.append(wrd)        #append the word
    wrd = ''               #clear old word
    wrd = wrd + i          #start new word
    idx = idx + 1            
    cnt = 0
    
  else:
    wrd = wrd + i          #building word
  cnt = cnt + 1
    
nwz.append(wrd)            #append remaining word
print(nwz)

Result


>'split and match'
['zzzzz', 'xxx', 'yyyyy']
Answered By: PS121-2

Consider a solution based on iterator and itertools.islice method:

import itertools

def split_by_space(s1, s2):
    chunks = s1.split()
    it = iter(s2)   # second string as iterator
    return [''.join(itertools.islice(it, len(c))) for c in chunks]


print(split_by_space('head eat or', 'aaaabbbcc'))  # ['aaaa', 'bbb', 'cc']
Answered By: RomanPerekhrest
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.