-3

Given two lists: list1 containing sentences and list2 containing words, I want to find the sentences of list1 having all the words belonging to list2.

list1 = ['p jthputmxy xpih t zdamz', 'l kefylcbfl tpij p jonvs', 'c olqlyfxew ksah p opjto', 'o cbfolbbwa fcha b xcruo', 'x iirvablmi dvqg i jjguy', 'c ocqlyfoew ksrh p opato', 'n cyjelcxxy xlip t kvrks', 'l kajltafti egei a bzzts', 'p ctjpltfxa xgia t bdrms', 'a cavalaria esta a norte']

list2 =  ['atacar', 'esperar', 'noite', 'base', 'sul', 'norte', 'cavalaria', 'esta', 'a', 'pato']

desired output = [False, False, False, False, False, False, False, False, False, True]

My code so far:

test = [all(i for i in list2) for f in list1]
return test

Only returns:

[True, True, True, True, True, True, True, True, True, True]
abc
  • 11,579
  • 2
  • 26
  • 51
Geo
  • 9
  • 5
  • Can you explain how you get your desired output? – cicolus Jan 03 '21 at 18:13
  • i'm not getting it. that' s why i´m asking for help – Geo Jan 03 '21 at 18:14
  • 1
    @Geo he means why your desired output is your desired output. – ssp Jan 03 '21 at 18:15
  • 1
    Why is `'l kajltafti egei a bzzts'` not true, even though it contains `a`? – Henry Jan 03 '21 at 18:15
  • Sorry for my bad english! It's for discover a keyword. in this example, the element True is Pato. its the keyword do decypher. its an college exercise – Geo Jan 03 '21 at 18:17
  • if 'pato' returns true, so should: 'norte', 'cavalaria', 'esta', 'a', – AndrewGraham Jan 03 '21 at 18:18
  • i want to give true, only for the sentence that contais words of list 2, in this case "a cavalaria esta a norte" – Geo Jan 03 '21 at 18:19
  • @Geo In that case you should update your question to reflect your requirements. The question as it stands is vague and not answerable. – cicolus Jan 03 '21 at 18:22
  • 1
    Your checks to see whether each string in `list2` is empty (False) or not (True); it repeats that check `len(list1)` times. You didn't make *any* check for those words being in the target sentences. – Prune Jan 03 '21 at 18:41

4 Answers4

1

You can convert list2 to a set and check if the set made with words in the sentence is a subset of your wordset.

>>> wordset = set(list2)
>>> res = [set(sentence.split()) <= wordset  for sentence in list1]
>>> res
[False, False, False, False, False, False, False, False, False, True]
abc
  • 11,579
  • 2
  • 26
  • 51
0
list1=['p jthputmxy xpih t zdamz', 'l kefylcbfl tpij p jonvs', 'c olqlyfxew ksah p opjto', 'o cbfolbbwa fcha b xcruo', 'x iirvablmi dvqg i jjguy', 'c ocqlyfoew ksrh p opato', 'n cyjelcxxy xlip t kvrks', 'l kajltafti egei a bzzts', 'p ctjpltfxa xgia t bdrms', 'a cavalaria esta a norte']
list2=['atacar', 'esperar', 'noite', 'base', 'sul',
    'norte', 'cavalaria', 'esta', 'a', 'pato']

results=[]
for i in list2:
    isin=False
    for l1item in list1:
        if i in l1item:
            isin=True
    results.append(isin)
results

This returns the following output (which makes sense to me)

[False, False, False, False, False, True, True, True, True, True]
AndrewGraham
  • 310
  • 1
  • 8
0

Following snippet code would help you to find the answers. compare to the other solutions, this would be executed faster. Thanks to @Olvin Roght for preparing the benchmark.


def match(words, sentences):
    c = []
    for sentence in sentences:
        for word in words:
            result = False
            if word in sentence:
                result = True
                break;
        c.append(result)
    return c

sentences = ['p jthputmxy xpih t zdamz', 'l kefylcbfl tpij p jonvs', 'c olqlyfxew ksah p opjto', 'o cbfolbbwa fcha b xcruo', 'x iirvablmi dvqg i jjguy', 'c ocqlyfoew ksrh p opato', 'n cyjelcxxy xlip t kvrks', 'l kajltafti egei a bzzts', 'p ctjpltfxa xgia t bdrms', 'a cavalaria esta a norte']

words = ['atacar', 'esperar', 'noite', 'base', 'sul','norte', 'cavalaria', 'esta', 'a', 'pato']

print(match(words, sentences))
Soroosh Khodami
  • 1,185
  • 9
  • 17
  • 1
    @SorooshKh, why are you proclaiming your solution to be faster than one with sets? Have you done any benchmarks? Your solution is far from perfect, cause you're reimplementing [`any()`](https://docs.python.org/3/library/functions.html#any) with your inner loop. – Olvin Roght Jan 04 '21 at 09:21
  • @OlvinRoght you are right, I didn't know any have the same implementation. So I think the performance is approximately the same. – Soroosh Khodami Jan 04 '21 at 09:43
  • 1
    @SorooshKh, no, I am **not right**, your option **is** much faster than other, but to be confident consider doing some [benchmarks](https://repl.it/@testerreplit/SpitefulWeirdIntercept) before. – Olvin Roght Jan 04 '21 at 11:50
0

all() function returns True if the variable is string too. So that's why you are getting everywehre trues, because you have everywhere a string in the list1.

So you are saying test = [True for f in list1]