从数组的元素中查找所有匹配的单词,保留这些单词,但删除不包含这些单词的元素

我正在尝试删除其中没有所有单词的array / json的所有元素。例如:

寻找单词:World Hold On

Array [
    {
       "title" : "Hold On World",
       "artist": "some guy"
    },
    { 
       "title" : "World, Hold On",
       "artist": "some guy"
    },
    {
       "title" : "World Hold On Now",
       "artist": "some guy"  
    },
    {
       "title" : "World Is Ending",
       "artist": "some guy"
    }
 ]

因此,使用此数组,应保存前三个元素,但应删除最后一个元素,我尝试过这样的操作:

def removeNonMatches(data, title):
    old = title.split(' ')
    new = '|'.join(old)
    p = re.compile(new, re.I)
    for x in data:
       if bool(p.search(r"\b("+p+")\b", x['title'], re.IGNORECASE)) == False:
            data.remove(x)
    return data

data = [
     {
       "title" : "Hold On World",
       "artist": "some guy"
    },
    { 
       "title" : "World, Hold On",
       "artist": "some guy"
    },
    {
       "title" : "World Hold On Now",
       "artist": "some guy"  
    },
    {
       "title" : "World Is Ending",
       "artist": "some guy"
    }
]

title = 'World Hold On'

new_data = removeNonMatches(data, title)
评论
香思松
香思松

您可以使用

data = [
    {
       "title" : "Hold On World",
       "artist": "some guy"
    },
    { 
       "title" : "World, Hold On",
       "artist": "some guy"
    },
    {
       "title" : "World Hold On Now",
       "artist": "some guy"  
    },
    {
       "title" : "World Is Ending",
       "artist": "some guy"
    }
 ]
words = ["World", "Hold", "On"]

keep =  [item for item in data
         if all(word in item["title"] for word in words)]
print(keep)

哪个产量

[{'title': 'Hold On World', 'artist': 'some guy'}, 
 {'title': 'World, Hold On', 'artist': 'some guy'}, 
 {'title': 'World Hold On Now', 'artist': 'some guy'}]
words = ["world", "hold", "on"]
keep =  [item for item in data
         if all(word in item["title"].lower() for word in words)]
点赞
评论