在python中获取包含子列表的元素

时间:2018-04-23 22:42:23

标签: python list substring sublist

我正在尝试搜索大量评论,并使用子列表中的字词返回评论。

sub = 'body'
submore = ['Olive', 'Personally', 'Hiding']
submore = [s.lower() for s in submore]
print submore


for s in submore:
    print "\n".join(s.lower() for s.lower(0 text.take(1000) if 
           submore.lower() in s.lower())

实施例。如果文本列表中的一个注释具有句子"我个人会",它将打印整个注释。

上面的代码适用于字符串但不适用于列表。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

将理解列表与any内置函数结合使用:

comments = [...arbitrary list of comments...]
comments = [c.lower() for c in comments] # put all comments in lowercase
words = [...arbitrary list of words...]
words = [w.lower() for w in words] # same for words

matches = [c for c in comments if any(w in c for w in words)]