2016-11-22 45 views
-1

我开始了一个循环,想要执行两个命令,但只执行最后一个命令。我需要做什么,这两个命令将被执行?循环中的两个命令

import re 
import string 
from string import punctuation 
doc_a = "Brocolli is good to eat. My brother likes to eat good brocolli, but not my mother." 
doc_b = "My mother spends a lot of time driving my brother around to baseball practice." 
doc_c = "Some health experts suggest that driving may cause increased tension and blood pressure." 
doc_d = "I often feel pressure to perform well at school, but my mother never seems to drive my brother to do better." 
doc_e = "Health professionals say that brocolli is good for your health." 
doc_set = [doc_a, doc_b, doc_c, doc_d, doc_e] 

for i in doc_set: 

    doc_set = re.sub(r'\d+', '', i) 


    doc_set = "".join(l for l in i if l not in string.punctuation) 


print(doc_set) 
+1

为什么你指定're.sub'结果'doc_set'? 're.sub'只修改列表中的一个元素。 –

+0

你覆盖第一个结果 – eri

+0

而不是'i = re.sub(r'\ d +','',i)'。 – L3viathan

回答

0

试试这个:

import re 
import string 
from string import punctuation 
doc_a = "Brocolli is good to eat. My brother likes to eat good brocolli, but not my mother." 
doc_b = "My mother spends a lot of time driving my brother around to baseball practice." 
doc_c = "Some health experts suggest that driving may cause increased tension and blood pressure." 
doc_d = "I often feel pressure to perform well at school, but my mother never seems to drive my brother to do better." 
doc_e = "Health professionals say that brocolli is good for your health." 
doc_set = [doc_a, doc_b, doc_c, doc_d, doc_e] 

new_doc_set = [] 
for i in doc_set: 
    i = re.sub(r'\d+', '', i) 
    i = "".join(l for l in i if l not in string.punctuation) 
    new_doc_set.append(i) 

print(new_doc_set)