curseword = ['fuxx', 'die', 'damn']
text = [ ['i','am','a','damn','boy']]
print(list(filter(lambda z:z!=[None],map(lambda x:(list(map(lambda y:y if x in y else None,text))),curseword))))
curseword = {'fuxx', 'die', 'damn'}
text = [ ['i','am','a','boy'] , [....] , [....] ]
new_text = map(int, [all(b not in curseword for b in i) for i in text])
In [10]: curseword = {'fuxx', 'die', 'damn'}
In [11]: text = [ ['i','am','a','boy'], ['die']]
In [21]: new_text = [int(bool(curseword.intersection(sent))) for sent in text]
In [22]: new_text
Out[22]: [0, 1]
就像你说的你想要不同的东西:
输出:
您可以将
cursewords
强制转换为set
,以提高查找效率,并使用列表理解,在较小的情况下比更通用的循环更有效:将
curseword
列表转换为一个集合,然后用户set.intersection
检查句子中的单词是否与cursword
重叠。你知道吗相关问题 更多 >
编程相关推荐