【发布时间】:2020-11-09 20:16:51
【问题描述】:
我对 python(和这个社区)相当陌生,这是一个从很久以前here 提出和回答的问题分支出来的问题
列表如下:
['hello', '...', 'h3.a', 'ds4,']
创建一个没有标点符号的新列表 x(并删除空元素)将是:
x = [''.join(c for c in s if c not in string.punctuation) for s in x]
x = [s for s in x if s]
print(x)
输出:
['hello', 'h3a', 'ds4']
但是,我如何才能仅从每个元素的开头和结尾删除所有标点符号?我的意思是,改为输出:
['hello', 'h3.a', 'ds4']
在这种情况下,保留 h3a 中的句点,但删除 ds4 末尾的逗号。
【问题讨论】:
-
s.strip(string.punctuation)将是一个起点。
标签: python list punctuation