我有一个包含词频的元组列表和一个要消除的单词列表。如何避免循环并从列表中删除元组?
data = [('the',23),('for',15),('so',10),('micro',10),('if',10),('macro',10)]
words = ['so','is','for','if'] # unique
indice =[]
# %%
for ii in range(len(data)):
for jj in range(len(words)):
if words[jj]==data[ii][0]:
print(words[jj]+ ': found')
indice.append(ii)
# del data[indice] # doesn't work
# data.remove(indice) # doesn't work
Run Code Online (Sandbox Code Playgroud)
我会将单词列表转换为一组以便更快地查找,然后使用列表理解:
wordset = set(wordset)
[item for item in data if item[0] not in wordset]
Run Code Online (Sandbox Code Playgroud)
这输出:
[('the', 23), ('micro', 10), ('macro', 10)]
Run Code Online (Sandbox Code Playgroud)