python关于split的用法

CMCCEDU2 2017-12-25 08:37:38

有两个问题:
一是按split两种分割法分割了 多出来的这些引号怎么消 我检验了列表的长度为7 但是我只想要那五个单词

还有就是我的这些是根据给定的句子进行分割, 假如句子不知道, 但是还是需要分割只要字母, 有没有其他更直白的方法 ,我觉得把所有标点符号都打上去的话太麻烦了

新手小白,第一次发问题 希望大神解答一下

...全文
792 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nozdormu-long 2018-01-26
  • 打赏
  • 举报
回复
list5='hello
  • 打赏
  • 举报
回复
list5="hello,can you hear me?"
import re
from string import punctuation
a=re.split(r'\W+', list5.strip(punctuation))
print(a)
for i in range(len(a)):
	print(a[i])
AlbertS 2017-12-26
  • 打赏
  • 举报
回复
引用 1 楼 xpresslink 的回复:

list5 = "hello, can you hear me?"
>>> from string import punctuation
>>> re.split(r'\W+', list5.strip(punctuation))
['hello', 'can', 'you', 'hear', 'me']

>>> re.findall(r'(\w+)', list5)
['hello', 'can', 'you', 'hear', 'me']

高手,佩服
CMCCEDU2 2017-12-26
  • 打赏
  • 举报
回复
引用 1 楼 混沌鳄鱼的回复:

list5 = "hello, can you hear me?"
>>> from string import punctuation
>>> re.split(r'\W+', list5.strip(punctuation))
['hello', 'can', 'you', 'hear', 'me']

>>> re.findall(r'(\w+)', list5)
['hello', 'can', 'you', 'hear', 'me']

谢谢(๑°3°๑) 我昨天自己想的时候干脆把键盘上的标点全部打上去了 然后把所有标点替换成空格 split按空格进行分割
混沌鳄鱼 2017-12-25
  • 打赏
  • 举报
回复

list5 = "hello, can you hear me?"
>>> from string import punctuation
>>> re.split(r'\W+', list5.strip(punctuation))
['hello', 'can', 'you', 'hear', 'me']

>>> re.findall(r'(\w+)', list5)
['hello', 'can', 'you', 'hear', 'me']

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧