【发布时间】:2020-02-26 20:20:36
【问题描述】:
所以我的任务是这样的:
编写一个程序,显示在文件 uniq_words.txt 中找到的所有唯一单词的列表。 按字母顺序和小写字母顺序打印您的结果。提示:将单词存储为集合的元素; 使用 string 模块中的 string.punctuation 删除标点符号。
目前,我拥有的代码是:
def main():
import string
with open('uniq_words.txt') as content:
new = sorted(set(content.read().split()))
for i in new:
while i in string.punctuation:
new.discard(i)
print(new)
main()
如果我这样运行代码,它会进入一个无限循环,一遍又一遍地打印唯一的单词。在我的集合中仍有 sre 单词显示为“值”。或“从不/有”。如何使用 string.punctuation 模块删除标点符号?还是我从错误的方向接近这个?非常感谢任何建议!
编辑:该链接对我没有帮助,因为给出的方法在列表中不起作用。
【问题讨论】:
-
这能回答你的问题吗? Best way to strip punctuation from a string
-
@GiftZwergrapper 我之前实际上已经阅读过那篇文章,但我认为它不能解决我的问题。
标签: python if-statement while-loop infinite-loop