本文介绍了从Python列表项中删除标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似的列表
['hello', '...', 'h3.a', 'ds4,']
这应该变成
['hello', 'h3a', 'ds4']
,我只想删除标点符号,使字母和数字保持完整.标点符号是string.punctuation
常量中的任何内容.我知道这很简单,但是我对python有点了解,所以...
and i want to remove only the punctuation leaving the letters and numbers intact.Punctuation is anything in the string.punctuation
constant.I know that this is gunna be simple but im kinda noobie at python so...
谢谢,giodamelio
Thanks,giodamelio
推荐答案
假设您的初始列表存储在变量x中,则可以使用以下方法:
Assuming that your initial list is stored in a variable x, you can use this:
>>> x = [''.join(c for c in s if c not in string.punctuation) for s in x]
>>> print(x)
['hello', '', 'h3a', 'ds4']
要删除空字符串,请执行以下操作:
To remove the empty strings:
>>> x = [s for s in x if s]
>>> print(x)
['hello', 'h3a', 'ds4']
这篇关于从Python列表项中删除标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!