Python 字符串拆分问题
问题:通过作为列表传入的分隔符字符将字符串拆分为单词列表 字符串:Python 字符串拆分问题,python,string,split,Python,String,Split,问题:通过作为列表传入的分隔符字符将字符串拆分为单词列表 字符串:“洪水过后……所有的颜色都出来了。” 所需输出:['After','the','flood','all','the','colors','come','out'] 我编写了以下函数-注意,我知道有更好的方法可以使用一些python内置函数拆分字符串,但为了学习,我想我会这样做: def split_string(source,splitlist): result = [] for e in source:
“洪水过后……所有的颜色都出来了。”
所需输出:['After','the','flood','all','the','colors','come','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', '', '', '', '', '', '', '', '', '']
for i, e in enumerate(source):
...
我不明白为什么“出来”不被分成“来了”和“出来”两个独立的词。这就像两个单词之间的空白字符被忽略一样。我认为剩余的输出是垃圾,源于与“出来”问题相关的问题
编辑:
我按照@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']
您不需要内部循环调用。这就足够了:
def split_string(source,splitlist):
result = []
for e in source:
if e in splitlist:
end = source.find(e)
result.append(source[0:end])
source = source[end+1:]
return result
在将源[:end]添加到列表之前,通过检查源[:end]是否为空字符串,可以消除“垃圾”(即空字符串)。您似乎期望:
source = tmp[start:]
修改外部for循环正在迭代的源代码。它不会-循环将继续遍历您给它的字符串,而不是现在使用该名称的任何对象。这可能意味着您要使用的角色可能不在源代码
的剩余部分中
不要尝试这样做,而是通过以下方式跟踪字符串中的当前索引:
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', '', '', '', '', '', '', '', '', '']
for i, e in enumerate(source):
...
你要添加的内容总是source[lasti+1:i]
,你只需要跟踪lasti
为什么要做太多的事情,
就这么简单,试试。。
str.split(strSplitter,intMaxSplitCount)
intMaxSplitCount是可选的
在你的情况下,如果你想避免。。。
一个是你可以替换它,比如str.replace(“.”,“”,3)
3是可选的,它只会替换前3个点
简言之,你得跟着,
打印((str.replace(“.”,“,”,3)).split(“”)
它将打印您想要的内容
我执行了死刑
这里的“a”是您的输入字符串。更简单的方法,至少看起来更简单
import string
def split_string(source, splitlist):
table = string.maketrans(splitlist, ' ' * len(splitlist))
return string.translate(source, table).split()
你可以签出和我认为如果你使用正则表达式,如果你只需要上面给出的字符串中的单词,你可以很容易地得到它
>>> import re
>>> string="After the flood ... all the colors came out."
>>> re.findall('\w+',string)
['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']
感谢大家提供的精彩解决方案。我之所以选择这个函数,是因为它迫使我学习逻辑,而不是使用预先构建的函数。显然,如果我要写商业代码,我不会再发明轮子,但出于学习的目的,我会用这个答案。谢谢大家的帮助。