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 に答える