Removing Punctuation From Python List Items

后端 未结 5 707
耶瑟儿~
耶瑟儿~ 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:18

    import string
    
    print ''.join((x for x in st if x not in string.punctuation))
    

    ps st is the string. for the list is the same...

    [''.join(x for x in par if x not in string.punctuation) for par in alist]
    

    i think works well. look at string.punctuaction:

    >>> print string.punctuation
    !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
    

提交回复
热议问题