编写for循环以删除标点符号

时间:2018-06-27 09:14:51

标签: python python-3.x

我的任务是编写一个for循环,以删除字符串列表中的标点符号,并将答案存储在新列表中。我知道如何用一个字符串来做到这一点,但是不是一个循环。

例如:phrases = ['hi there!', 'thanks!']

import string
new_phrases = []
for i in phrases:
    if i not in string.punctuation

这时我有点卡住了。我要追加吗?我已经尝试过yieldreturn,但意识到这是针对函数的。

8 个答案:

答案 0 :(得分:1)

您可以更新当前列表,也可以将新值添加到另一个列表中。更新会更好,因为它需要恒定的空间,而append需要O(n)的空间。

phrases = ['hi there!', 'thanks!']
i = 0
for el in phrases:
    new_el = el.replace("!", "")
    phrases[i] = new_el
    i += 1
print (phrases)

将给出输出:['hi there','thanks']

答案 1 :(得分:1)

尝试一下:

import re
new_phrases = []

for word in phrases:
    new_phrases.append(re.sub(r'[^\w\s]','', word))

这使用正则表达式库将所有标点符号转换为“空白”字符串。本质上,将其删除

答案 2 :(得分:1)

如果短语包含任何标点符号,则将其替换为“”并附加到new_phrases

import string
new_phrases = []
phrases = ['hi there!', 'thanks!']
for i in phrases:
    for pun in string.punctuation:
        if pun in i:
            i = i.replace(pun,"")
    new_phrases.append(i)
print(new_phrases)

输出

['hi there', 'thanks']

答案 3 :(得分:1)

您可以使用re模块和list comprehension在一行中完成此操作:

phrases = ['hi there!', 'thanks!']

import string
import re

new_phrases = [re.sub('[{}]'.format(string.punctuation), '', i) for i in phrases]
new_phrases
#['hi there', 'thanks']

答案 4 :(得分:0)

按照您的思维方式,我会这样:

for word in phrases: #for each word
    for punct in string.punctuation: #for each punctuation
        w=w.replace(punct,'') # replace the punctuation character with nothing (remove punctuation)
    new_phrases.append(w) #add new "punctuationless text" to your output

答案 5 :(得分:0)

我建议您对输入列表的每个字符串使用功能强大的translate()方法,这似乎很合适。它提供以下代码,通过列表理解遍历输入列表,该过程简短且易于阅读:

import string

phrases = ['hi there!', 'thanks!']
translationRule = str.maketrans({k:"" for k in string.punctuation})
new_phrases = [phrase.translate(translationRule) for phrase in phrases]

print(new_phrases)
# ['hi there', 'thanks']

答案 6 :(得分:0)

或仅允许使用空格和字母:

phrases=[''.join(x for x in i if x.isalpha() or x==' ') for i in phrases]

现在:

print(phrases)

是:

['hi there', 'thanks']

答案 7 :(得分:-2)

您应该使用列表理解

new_list = [process(string) for string in phrases]