【发布时间】:2019-01-24 17:39:22
【问题描述】:
我想从一个句子中删除逗号并将所有其他单词(a-z)分开并一个一个打印出来。
a = input()
b=list(a) //to remove punctuations
for item in list(b): //to prevent "index out of range" error.
for j in range(len(l)):
if(item==','):
b.remove(item)
break
c="".join(b) //sentence without commas
c=c.split()
print(c)
我的输入是:
The university was founded as a standard academy,and developed to a university of technology by Habib Nafisi.
当我删除逗号时:
... founded as a standard academyand developed to a university...
当我拆分单词时:
The
university
.
.
.
academyand
.
.
.
我能做些什么来防止这种情况发生? 我已经尝试过替换方法,但它不起作用。
【问题讨论】:
-
你期望的输出是什么?
-
你试过正则表达式(docs.python.org/3/library/re.html#re.split)吗?
-
@JimWright "academy" "and" 分开
-
@janbrohl 对我不起作用。
标签: python