編寫帶有上面字符串列表的函數list_of_words,並返回刪除了所有空格和標點符號的單個列表的列表(除了撇號/單引號)。在python中使用strip()
我的代碼刪除句號和空格,但不包含逗號或感嘆號。
def list_of_words(list_str):
m = []
for i in list_str:
i.strip('.')
i.strip(',')
i.strip('!')
m = m+i.split()
return m
print(list_of_words(["Four score and seven years ago, our fathers brought forth on",
"this continent a new nation, conceived in liberty and dedicated",
"to the proposition that all men are created equal. Now we are",
" engaged in a great civil war, testing whether that nation, or any",
"nation so conceived and so dedicated, can long endure!"])
我需要使用strip()或split()方法,而不是replace方法。 – 2015-02-12 07:10:48
簡短版本:'返回[word.strip('。,!')爲list_str中的部分part.split()]中的單詞' – Matthias 2015-02-12 09:59:46