Removing Punctuation From Python List Items

后端 未结 5 709
耶瑟儿~
耶瑟儿~ 2020-11-29 08:56

I have a list like

[\'hello\', \'...\', \'h3.a\', \'ds4,\']

this should turn into

[\'hello\', \'h3a\', \'ds4\']

5条回答
  •  臣服心动
    2020-11-29 09:22

    Use string.translate:

    >>> import string
    >>> test_case = ['hello', '...', 'h3.a', 'ds4,']
    >>> [s.translate(None, string.punctuation) for s in test_case]
    ['hello', '', 'h3a', 'ds4']
    

    For the documentation of translate, see http://docs.python.org/library/string.html

提交回复
热议问题