for x,y in words:
for z in x:
if z in stopwords:
del x[x.index(z)]
これは私のコードです。delはリストから何も削除していないようです
(list of words, metadata)
私のコードの目的は、単語のリストからすべてのストップワードを削除することです:言葉のデータはタプルは次のようになりタプルのリストです。 これは唯一の問題は、ストップワードが後で削除されないことです...
私は間違って何をしましたか? 私はすでに
x.pop(x.index(z))
でそれを実行しようとしましたが、それは違いを確認していないようです。例えば
stopwords = set(stopwords) # just so "in" checks are faster
result = [([word for word in x if word not in stopwords], y) for x, y in words]
:
>>> stopwords = ['stop']
>>> words = [(['hello', 'you', 'stop'], 'somemeta')]
>>> stopwords = set(stopwords) # just so "in" checks are faster
>>> result = [([word for word in x if word not in stopwords], y) for x, y in words]
>>> result
[(['hello', 'you'], 'somemeta')]
注あなたは、一般的にリストあなた」を変更しないでください
反復処理中にリストからデータを削除するのは良い考えではないため、定義されていない動作が発生する可能性が高くなります。代わりにあなたの問題をリスト内包として定式化し、あなたの基準に合致する新しいリストを作成しようとします。 –
単語とストップワードの例を挙げてください – nacho