問題
for sentence in sentences: # 按空格劃分單詞並過濾空串
sentences_words.append(filter(lambda x : x.strip(), sentence.split(" ")))
for words in sentences_words: # 對每句中的單詞
for i in range(len(words) - 2):
最近在用Python的filter過濾list中的元素的時候。以上代碼報錯在於3.x的Python filter返回的是iterator類型,需要用list()將其轉化過來
解決
用list()函數獲得所有結果並返回list