简体   繁体   中英

python for take a list of sentences and a list of words, return the index of the sentence if there is a match

I am writing a function that takes a list of sentences and a list of words, then checks to see if the words (or phrases) are in the sentences. If they are, it appends the index of that sentence to a result list. it would look something like this:

input

sentences = ['a long brown fox', 'i never knew One fox']
words = ['long', 'knew One', 'fox', 'never fox']

output

[[0],[1],[0,1],[1]]

this is because the first word only appears in the first sentence, the second phrase only appears in the second sentence, and the third word appears in both.

I want to use the any function but I'm not sure how to use it and return the indices. something along the lines of

def textQueries(sentences, queries):
    matches = []
    for x in queries:
        if x in sentences and x not in matches:
            matches.append(x)
    return result_array

is what i have but not very efficient. any suggestions?

Try list comprehensions: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

sentences = ['a long brown fox','i never knew One fox']
words = ['long', 'knew One', 'fox']

indices = [ [i for i, sentence in enumerate( sentences ) if word in sentence] for word in words]
print( *indices, sep="\n" )

This is equivalent to:

indices = []
for word in words:
    sentence_indices = []
    for i, sentence in enumerate(sentences):
        if word in sentence:
            sentence_indices.append( i )
    indices.append( sentence_indices )
print( *indices, sep="\n" )

Edited for others:

import re
sentences = ['a long brown fox','i never knew One fox']
words = ['long', 'knew One', 'fox', 'never fox']
indices = [ [ i for i,sentence in enumerate( sentences ) if re.search( '.+'.join( word.split()), sentence)] for word in words]
print( *indices, sep="\n" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM