0

Surpose the string:

'abcdeabcdeabcdeabcdeabcdeabcde' 

split by :

'cdebbe' 

output:

'ab c d e a b cdea b cd e abcdeabcde'

This means split string

'abcdeabcdeabcdeabcdeabcdeabcde' 

by indexes

[2,3,4,6,10,14]

How to do this by Python? This question is similar to Split a list into parts based on a set of indexes in Python If follow the answers the out put is :

['ab', 'c', 'd', 'ea', 'bcde', 'abcd', 'eabcdeabcdeabcde']

isnt the expected output.

Community
  • 1
  • 1
Tom
  • 83
  • 1
  • 7

3 Answers3

2
'abcdeabcdeabcdeabcdeabcdeabcde'.split('cdebbe')

But your string does not contain 'cdebbe' substring, so result is a list with one string:

['abcdeabcdeabcdeabcdeabcdeabcde']
Alexey Guseynov
  • 5,116
  • 1
  • 19
  • 29
1
s = 'abcdeabcdeabcdeabcdeabcdeabcde'
new = []

for i,char in enumerate('cdebbe'):
    new.append(s.split(char)[:i+1])

print new
>>[['ab'], ['abc', 'eabc'], ['abcd', 'abcd', 'abcd'], ['a', 'cdea', 'cdea', 'cdea'], ['a', 'cdea', 'cdea', 'cdea', 'cdea'], ['abcd', 'abcd', 'abcd', 'abcd', 'abcd', 'abcd']]

This will split your string by all letters in 'cdebbe' independently.

doing a bit of manipulation to this we can get the response you require. But it's not exactly what you expected.

new2 = []
for i, x in enumerate(new):
    new2.append(new[i][-1])

print new2 
>>['ab', 'eabc', 'abcd', 'cdea', 'cdea', 'abcd']

Quick explanation: 'ab' is the first part of the string split at 'c', 'eabc' is the second part of the string when split on 'd' etc etc

Daniel Lee
  • 7,189
  • 2
  • 26
  • 44
1

This quite matches your expected output, but I don't know what's the logic when 'cdebbe' reaches the end.

s = 'abcdeabcdeabcdeabcdeabcdeabcde'
split = 'cdebbe'

result = []
for c in split:
    left, center, s = s.partition(c)
    if left:
        result.append(left)
    result.append(center)

print(' '.join(result)) # ab c d e a b cdea b cd e
Francisco
  • 10,918
  • 6
  • 34
  • 45