检查字符串python中是否存在列表中的任何单词

时间:2015-07-29 12:17:51

标签: python

我在mongo集合中有近5万个文档,有点像这样:

{"title":"sample title sample title",
 "content":"test content test content",
 "reply":{
           "replyContent":"sample reply content test"
          }
}

我有一个像这样的单词:

wordArr = ["sample","test"]

如果在我的文档集合中出现任何单词形式wordArr,我需要匹配。我必须遍历集合中的每个文档,并且必须搜索数组id中给出的任何单词是否存在于任一字段中,即title,content和replyContent

2 个答案:

答案 0 :(得分:0)

假设您的mongo集合在字典中,以下内容应该有效(对不起,我没有使用mongo集合的经验。

dict = {"title":"sample title sample title",
        "content":"test content test content",
        "reply":{"replyContent":"sample reply content test"}
       }

wordArr = ["sample","test"]

for word in wordArr:

    for key, value in dict.iteritems():

        if word in value:
            print 'Word: `%s` present in `%s`: %s' % (word, key, value)

        if key=='reply':
            for key2,value2 in value.iteritems():
                print 'Word `%s` present in `%s`: %s' % (word, key2, value2)

这将为您提供以下输出:

> python test.py
Word `sample` present in `replyContent`: sample reply content test
Word: `sample` present in `title`: sample title sample title
Word: `test` present in `content`: test content test content
Word `test` present in `replyContent`: sample reply content test

答案 1 :(得分:0)

如果您只想返回True或False:

d = {"title": "sample title sample title",
     "content": "test content test content",
     "reply": {
         "replyContent": "sample reply content test"
     }
     }

word_set = {"sample", "test"}
def is_present(d, st):
    for v in d.values():
        if isinstance(v, dict):
            for val in d.values():
                if any(word in st for s in val for word in s.split()):
                    return True
        else:
            if any(word in word_set for word in v.split()):
                return True
    return False

print(is_present(d,word_set))

如果您有任意级别的嵌套,则可能需要嵌套方法