0

下面是我的代码。我正在尝试查找出现在我的页面中的所有查询。但我找不到解决方案来检查它们是否以与指定查询相同的顺序出现。有什么解决办法吗?

def search_query(query, pages):

    if len(query) > 10:
        return 
    if len(pages) > 1000:
        return 

    valid_pages = 0

    for p in pages:
        valid_query = 0
    for q in query:
        if q in p:
            valid_query += 1
    if valid_query == len(query):
        valid_pages += 1
print valid_pages 

query = ["the","new","store"]
pages = ["the new store is in san francisco",
"the boy enters a new and cool store",
"this boy enters a new store",
"The new store is in the city",
"the newstore is a brand",
"there is newton in the store"]

结果是 5。没关系,但如果我想按查询顺序检查,它必须是 4。谢谢!

4

1 回答 1

0
def search_query(query, pages):
    valid_pages = 0
    for p in pages:
        i = 0
        for q in query:
            j = p.find(q, i)
            if j < 0:
                break
            i = j + 1
        else:
            valid_pages += 1
    return valid_pages 
于 2013-06-13T13:32:13.537 回答