简体   繁体   English

检查列表中是否有任何单词出现在字符串python中

[英]Check if any word in the list present in string python

I have nearly 50k documents in a mongo collection somewhat like this: 我在mongo集合中有将近5万个文档,如下所示:

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

and I have an array of words something like this: 我有一系列这样的单词:

wordArr = ["sample","test"]

I need to match if any word form wordArr present in my collection of document. 我需要匹配文档集合中是否存在任何单词形式wordArr。 I have to iterate over each document from the collection and have to search if any of the word given in array id present in either of the fields ie title , content and replyContent 我必须遍历集合中的每个文档,并且必须搜索是否在任何字段(即title,content和replyContent)中存在的数组ID中给出的任何单词

The following should work assuming your mongo collection is in a dictionary (sorry I have no experience with mongo collections. 假设您的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)

This will give you the following output: 这将为您提供以下输出:

> 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

If you just want to return True or False: 如果只想返回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))

If you have arbitrary levels of nesting you might need a nested approach 如果您有任意级别的嵌套,则可能需要嵌套方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查Python中的列表中是否存在DataFrame字符串列的第一个单词? - How to check if first word of a DataFrame string column is present in a List in Python? 检查字符串中的字符是否包含在列表的任何单词中 - Check if the characters in a string are contained in any of the word of a list 检查字符串是否存在于同一子列表 python - Check if string present in same sub list python 检查python中列表的元素中是否存在列表中的任何元素 - Check if any element in a list is present in the elements of a list in python 如何检查列表 1 中的任何元素是否不存在于列表 2 中? - Python - How to check if any element in list 1 is not present in list 2? - python Python - 删除列表中所有以单词/字符串开头的行 - Python - Remove all the lines starting with word/string present in a list Python - 如果任何 substring 存在于另一个列表中,则返回字符串列表 - Python - Return list of string if any substring is present in another list 检查一个列表中的一项是否存在于另一个字符串列表中 - Check if an item of one list present in another list of string Python [Python]检查列表中的任何字符串是否包含另一个列表中的任何字符串 - [Python]Check if any string in a list is contains any string in another list 蟒蛇。 使用单词列表中的任何单词分割字符串 - Python. Split string using any word from a list of word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM