简体   繁体   中英

Removing Punctuation From Python List Items

I have a list like

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

this should turn into

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

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...

Thanks, giodamelio

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']

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

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

制作新列表:

[re.sub(r'[^A-Za-z0-9]+', '', x) for x in list_of_strings]

In python 3+ use this instead:

import string
s = s.translate(str.maketrans('','',string.punctuation))

请注意 string.punctuation 在英语中有效,但可能不适用于具有其他标点符号的其他语言。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM