问题:将一个字符串按照传入的分隔符列表拆分为单词列表。
字符串:
我无法理解为什么“came out”没有被分割成两个单独的单词“came”和“out”。就好像这两个单词之间的空格被忽略了一样。我认为输出结果的其余部分是由于“came out”问题所导致的垃圾数据。 编辑: 我按照@Ivc的建议编写了以下代码:
字符串:
"After the flood ... all the colors came out."
期望输出:['After','the','flood','all','the','colors','came','out']
我编写了以下函数——请注意,我知道使用一些Python内置函数可以更好地拆分字符串,但是为了学习,我打算采用这种方式进行:def split_string(source,splitlist):
result = []
for e in source:
if e in splitlist:
end = source.find(e)
result.append(source[0:end])
tmp = source[end+1:]
for f in tmp:
if f not in splitlist:
start = tmp.find(f)
break
source = tmp[start:]
return result
out = split_string("After the flood ... all the colors came out.", " .")
print out
['After', 'the', 'flood', 'all', 'the', 'colors', 'came out', '', '', '', '', '', '', '', '', '']
我无法理解为什么“came out”没有被分割成两个单独的单词“came”和“out”。就好像这两个单词之间的空格被忽略了一样。我认为输出结果的其余部分是由于“came out”问题所导致的垃圾数据。 编辑: 我按照@Ivc的建议编写了以下代码:
def split_string(source,splitlist):
result = []
lasti = -1
for i, e in enumerate(source):
if e in splitlist:
tmp = source[lasti+1:i]
if tmp not in splitlist:
result.append(tmp)
lasti = i
if e not in splitlist and i == len(source) - 1:
tmp = source[lasti+1:i+1]
result.append(tmp)
return result
out = split_string("This is a test-of the,string separation-code!"," ,!-")
print out
#>>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code']
out = split_string("After the flood ... all the colors came out.", " .")
print out
#>>> ['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']
out = split_string("First Name,Last Name,Street Address,City,State,Zip Code",",")
print out
#>>>['First Name', 'Last Name', 'Street Address', 'City', 'State', 'Zip Code']
out = split_string(" After the flood ... all the colors came out...............", " ."
print out
#>>>['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']