Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用split()在不同标点处分割字符串_Python_String_Split - Fatal编程技术网

Python 使用split()在不同标点处分割字符串

Python 使用split()在不同标点处分割字符串,python,string,split,Python,String,Split,我试图把字符串分成单词,去掉空格和标点符号 我尝试使用split()方法,一次传递所有标点符号,但结果不正确: >>> test='hello,how are you?I am fine,thank you. And you?' >>> test.split(' ,.?') ['hello,how are you?I am fine,thank you. And you?'] 实际上,我已经知道如何使用正则表达式来实现这一点,但我想知道如何使用split()

我试图把字符串分成单词,去掉空格和标点符号

我尝试使用
split()
方法,一次传递所有标点符号,但结果不正确:

>>> test='hello,how are you?I am fine,thank you. And you?'
>>> test.split(' ,.?')
['hello,how are you?I am fine,thank you. And you?']

实际上,我已经知道如何使用正则表达式来实现这一点,但我想知道如何使用
split()
实现这一点。请不要给我一个正则表达式解决方案。

如果您想基于多个分隔符拆分字符串,如您的示例中所示,您需要使用
re
模块,尽管您有奇怪的反对意见,如下所示:

>>> re.split('[?.,]', test)
['hello', 'how are you', 'I am fine', 'thank you', ' And you', '']
使用
split
可以得到类似的结果,但是您需要为每个字符调用split一次,并且需要迭代上一次分割的结果。这是可行的,但它是u-g-l-y:

>>> sum([z.split() 
... for z in sum([y.split('?') 
... for y in sum([x.split('.') 
... for x in test.split(',')],[])], [])], [])
['hello', 'how', 'are', 'you', 'I', 'am', 'fine', 'thank', 'you', 'And', 'you']

这使用
sum()
将上一次迭代返回的列表展平。

这是我能想到的最好的方法,而无需使用re模块:

"".join((char if char.isalpha() else " ") for char in test).split()

由于您不想使用re模块,因此可以使用以下方法:

 test.replace(',',' ').replace('.',' ').replace('?',' ').split()

您可以编写一个函数来扩展
.split()
的用法:

试试看:

>>multi_split('你好,你好吗?我很好,谢谢。你呢?'、'、.?'))
[‘你好’、‘你好’、‘你’、‘我’、‘很好’、‘谢谢’、‘你’、‘你’]


这将更加清晰,并可用于其他情况。

larsks答案的修改版本,您不需要自己键入所有标点符号:

import re, string

re.split("[" + string.punctuation + "]+", test)
['hello', 'how are you', 'I am fine', 'thank you', ' And you', '']

为Necroping道歉-此线程作为非正则表达式拆分句子的第一个结果出现。鉴于我必须为我的学生想出一个非Python特定的方法,而且这个线程没有回答我的问题,我想我会与大家分享以防万一

代码的要点是不使用库(而且在大文件上很快):

输出:

['George', 'Bernard-Shaw', 'was', 'a', 'fine', 'chap', "I'm", 'sure', '-', 'who', 'can', 'really', 'say']

我在大约半个小时内写了这篇文章,所以我相信逻辑可以被澄清。我也承认,它可能需要额外的逻辑来正确处理诸如连字符之类的警告,因为它们的使用与倒逗号之类的东西相比是不一致的。是否有任何模块可以正确执行此操作?

保存标点符号或其他分隔符的简单方法是:

import re

test='hello,how are you?I am fine,thank you. And you?'

re.findall('[^.?,]+.?', test)
结果:

['hello,', 'how are you?', 'I am fine,', 'thank you.', ' And you?']

也许这可以帮助别人。

所以你坚持用扳手钉钉子,而锤子就在手边。为什么?我不想对OP有任何不尊重,但我认为这类问题应该有一个标签,无论出于何种原因(有时是有效的),适当的工具都会被冷落,它们不时出现。也许
luddism
?试试C#“你好吗?我很好,谢谢。你呢?”.Split(“,?”.tocharray(),StringSplitOptions.removeMptyEntries);不要让任何人阻止你探索简单文本操作的非正则表达式方法。通过使用字符串方法、itertools.groupby和实际编写函数(!),我们中的一些人几乎从不使用正则表达式,作为交换,我们可以编写漂亮、干净、易于调试的Python.oo,这是另一种方法,尽管它没有使用显式的拆分字符列表……这太棒了。不过,与使用re.split相比,它的效率要低一些。请不要使用
sum()
来展平列表--。在这种特殊情况下,更是如此,因为a将从一开始就消除展平的必要性。如果您认为替代解决方案更适合问题,欢迎您发布。只要OP没有解释为什么不应使用
re
,我就不会发布答案,因为我还不明白这个问题的目的。不过,我最后一条评论中的第二个链接显示了另一种解决方案。test='您好,你好吗?我很好,谢谢。那么您呢?对于测试中的x:如果不是x.isalpha():test=test.replace(x,“”)test=test.split()打印测试
import re

test='hello,how are you?I am fine,thank you. And you?'

re.findall('[^.?,]+.?', test)
['hello,', 'how are you?', 'I am fine,', 'thank you.', ' And you?']