t.a.
t.a.

Reputation: 1

manually splitting a string in python - issue with the last delimiter

I wrote this:

def split(line,delim):
    s=[]
    j=0
    for i in range (len(line)-1):
        if delim== line [i]:
            s.append(line[j:i])
            j=i+1
    s.append (line[j:])
    return s

but when I put a line and the end-letter as the delim, it return also the delim like this:

split('bndghsjhskashakhs', 's')

['bndgh', 'jh', 'ka', 'hakhs']

Upvotes: 0

Views: 1753

Answers (5)

Akap official
Akap official

Reputation: 41

Hi I have a way for you...
Have done this without a single use of inbuilt split function

def split_string(string,divider):
  parts = []
  last = ""
  reverse = string[::-1]
  for i in string:
      if i == divider:
          parts.append(last)
          last = ""
      else:
          last = last + i
  reverse = reverse + string[0]
  first = ""
  for x in reverse:
          if x != divider:
              first = first + x
          else:
              break
  parts.append((first[::-1]))
  return (parts)

Now just print this

print(split_string("bndghsjhskashakhs","s"))

Upvotes: 0

eumiro
eumiro

Reputation: 212835

Problem is in your:

for i in range (len(line)-1):

This iterates from the first character to the before-last character. It ignores the last character. Change it to:

for i in range (len(line)):

Now it returns ['bndgh', 'jh', 'ka', 'hakh', ''].

Anyway, as the other posters write, you could use the standard .split() function.

If you want to remove the empty elements, you can include a filter at the end:

return [ item for item in s if item ] 

instead of

return s

or directly with the standard split without your function:

[ item for item in line.split('s') if item ]

or the shortest version:

filter(None, line.split('s'))

Upvotes: 3

TyrantWave
TyrantWave

Reputation: 4663

First off, you should use the inbuild string.split('s') for something like this, it'll save any trouble.

The reason you're missing the last character is because of the:

for i in range (len(line)-1):

range will return 0..max-1, so you're actually stopping a character early.

Remove the -1 and it should work.

Upvotes: 1

Bruce
Bruce

Reputation: 7132

you should use native split rather than rewriting your own: 'bndghsjhskashakhs'.split('s')

Upvotes: 1

kanaka
kanaka

Reputation: 73089

How about:

>>> 'bndghsjhskashakhs'.split('s')
['bndgh', 'jh', 'ka', 'hakh', '']

Or if you don't want empty values:

>>> filter(lambda x: x, 'bndghsjhskashakhs'.split('s'))
['bndgh', 'jh', 'ka', 'hakh']

Upvotes: 0

Related Questions