循环中的两个命令

时间:2016-11-22 12:46:56

标签: python string loops

我开始循环并希望执行两个命令,但只执行最后一个命令。我需要做什么才能执行这两个命令?

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 个答案:

答案 0 :(得分: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)