2016-08-29 18 views
0

は私がやりたいことリスト言葉:Pythonの

List = ['iamcool', 'Noyouarenot'] 
stopwords=['iamcool'] 

を持っている私のリストからstowprdsを削除することです。私は、だから私の結果は、私は私が小さな何の事右もあるエラー

AttributeError: 'list' object has no attribute 'split' 

を受け付けております

result =['Noyouarenot'] 

あるべきスクリプト

query1=List.split() 
resultwords = [word for word in query1 if word not in stopwords] 
result = ' '.join(resultwords) 
return result 

以下でこれをacheiveしようとしています行方不明、お手伝いください。私はあらゆる助けに感謝します。

+0

if 'iamcool' inリスト:List.remove( 'iamcool') ' – depperm

+0

そのステップを削除して、' '単語がストップワードにない場合はリストに単語を入れてください '' – roganjosh

+3

Pythonがあなたに言っているようにリストを分割することはできません。 'split()'部分を削除して、@roganjoshが述べたように前進してください。 – dblclik

答えて

3

会員資格を確認する条件付きリストの理解度はstopwordsです。

print [item for item in List if item not in stopwords] 

またはfilter

print filter(lambda item: item not in stopwords, List) 

またはset操作は、あなたは速度差hereに私の答えを参照することができます。

print list(set(List) - set(stopwords)) 

出力 - >['Noyouarenot']

1

はここにあなたのエラーを修正スニペットです:あなたの入力リストとストップワードリストを想定し

lst = ['iamcool', 'Noyouarenot'] 
stopwords = ['iamcool'] 

resultwords = [word for word in lst if word not in stopwords] 
result = ' '.join(resultwords) 
print result 

別の可能な解決策は、順序、重複を気にしないでください。

print " ".join(list(set(lst)-set(stopwords)))