python - 不能让它成为一个列表 - python
问题描述
我搜索并回答,但找不到答案。
我无法将该 str 添加到列表中,如何将其打印为列表?
请指教。
punctuation = ['.','(',')','?',':',':',',','.','!','/','"',"'",'@','#','$','%','^','&','*']
tokenize = str(input("Please enter a sentence " ))
tokenize = "".join(char for char in tokenize if char not in punctuation)
print ("Tokenized:",tokenize.lower())
解决方案
您可以使用re.split
交替模式:
import re
punctuation = ['.','(',')','?',':',':',',','.','!','/','"',"'",'@','#','$','%','^','&','*']
tokenize = str(input("Please enter a sentence: " ))
print(re.split('|'.join(map(re.escape, punctuation)), tokenize))
样本输入和输出:
Please enter a sentence: Hello,World!foo:bar
['Hello', 'World', 'foo', 'bar']
推荐阅读
- python - 用公式逻辑地确定游戏结果
- rest - 如何使用 microsoft flow 获取任务的资源名称 - 项目在线连接器
- flutter-web - 在 Flutter Web 上制作响应式 Web
- javascript - 如何从 Rails 控制器操作中调用 javascript
- javascript - 当我接收数据时在 django 的后端显示关键错误
- excel - 如果 5 个单元格值更改为某个值,则需要取消隐藏一行
- python - Jupyter notebook 命令在命令行中被识别但不运行并冻结
- sql-server - 使用 TOP 和 UNION 时的意外结果
- python-3.x - 使用 os.walk 的循环只运行第一个循环
- flutter - StreamBuilder 仅在我使用按钮更改状态时重新呈现数据