我正在处理JSON文件中的数据以进行机器学习。数据是句子。句子被读入数组并完美地使用NLTK标记。因此,在每个句子数组中,都剩下类似['set', 'a', 'timer', 'for', '*int', '*unit_of_time']
的内容,这是完全正确的。我想删除所有包含''的元素。这可以在90%的时间内正确运行,但是我发现,如果连续有两个元素包含一个'',则第二个元素将被保留。因此,如果我运行:
words = ['set', 'a', 'timer', 'for', '*int', '*unit_of_time']
words = nltk.word_tokenize(pattern)
for word in words:
if '*' in word:
words.remove(word)
我留下了words = ['set', 'a', 'timer', 'for', '*unit_of_time']
,但应该留下`words = ['set','a','timer','for']循环成功删除了'* int',但没有删除' * unit_of_time'。
我做错了吗?我在Ubuntu 19.10上使用的是Python 3.7。
如果我可以提供任何其他信息,请告诉我。