Reputation: 115
Im having some problems trying to solve this question. Its from a practice exam and I just can't seem to get it right. Im supposed to write a python function that takes in a string and a delimiter, and return a list back with the string stripped of the delimiter. We are not allowed to use the split function or "any such function". The example we received in the question was this
StringToken("this is so fun! I love it!", "!")
Outputs
["this is so fun", "I love it"]
This is the code I made up, its super simple.
def tokenizer(string, tmp):
newStr = []
for i in range(len(string)):
if string[i] != tmp:
newStr.append(string[i])
return newStr
and the output is this
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 'o', ' ', 'f', 'u', 'n', ' ', 'I', ' ', 'l', 'o', 'v', 'e', ' ', 'i', 't']
How can I rejoin each word?
Upvotes: 1
Views: 768
Reputation: 1324
Here's an alternative that's a little shorter than the current answers:
def StringToken(string, tmp):
newStr = []
start = 0
for ind, char in enumerate(string):
if char == tmp:
newStr.append(string[start:ind])
start = ind + 1
return newStr
Output
>>> StringToken("this is so fun! I love it!", "!")
['this is so fun', ' I love it']
Edit: If you would like to remove leading or trailing spaces like in your example, that can be done using strip():
def StringToken(string, tmp):
newStr = []
start = 0
for ind, char in enumerate(string):
if char == tmp:
newStr.append(string[start:ind].strip())
start = ind + 1
return newStr
Output
>>> StringToken("this is so fun! I love it!", "!")
['this is so fun', 'I love it']
Upvotes: 0
Reputation: 9345
If you join all the elements in the list you will get a single string which may not be what you are looking for.
Create a string before append it to the list like;
>>> def StringToken(string, tmp):
newStrlist = []
newStr = ''
for i in range(len(string)):
if string[i] != tmp:
newStr += string[i]
elif newStr != '':
newStrlist.append(newStr)
newStr = ''
return newStrlist
... ... ... ... ... ... ... ... ... ...
>>> StringToken("this is so fun! I love it!", "!")
['this is so fun', ' I love it']
Upvotes: 3
Reputation: 191738
See comments in the code for a description.
def StringToken(string, tmp):
newStr = "" # A string to build upon
lst = [] # The list to return
for c in string: # Iterate over the characters
if tmp == c: # Check for the character to strip
if newStr != "": # Prevent empty strings in output
lst.append(newStr.strip()) # add to the output list
newStr = "" # restart the string
continue # move to the next character
newStr += c # Build the string
return lst # Return the list
Output
StringToken("this is so fun! I love it!", "!")
# ['this is so fun', 'I love it']
Upvotes: 0
Reputation: 19831
Instead of looping over the all the letters in the string, you can use find
to get the index of the next occurrence of the delimiter and then build your list accordingly:
def tokenizer(string, delim):
new_list = []
while True:
index = string.find(delim) # use find to next occurrence of delimiter
if index > -1:
new_list.append(string[:index])
string = string[index + len(delim):]
else:
new_list.append(string)
break # break because there is no delimiter present anymore
# remove whitespaces and trim the existing strings
return [item.strip() for item in new_list if item.strip()]
Usage:
>>> tokenizer("this is so fun! I love it!", "!")
["this is so fun", "I love it"]
Upvotes: 0
Reputation: 967
simply use join operator this will join entire list with a given delimiter. Here in this you can use empty delimiter ''. try:
a=['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 'o', ' ', 'f', 'u', 'n', ' ', 'I', ' ', 'l', 'o', 'v', 'e', ' ', 'i', 't']
''.join(a)
output will be
'This is so fun I love it'
Upvotes: -1