3
strings = ['I have a bird', 'I have a bag and a bird', 'I have a bag']
words = ['bird','bag']

I want to find the string that includes both bird and bag in the list strings, regardless of order. So the result for only the second element in strings should be true and the rest should be false.

The output I want:

False
True
False

words do not necessarily need to be stored in list, and I know that regex could do a similar thing but I would prefer to use other ways than regex because my words are mandarin chinese which requires some complicated use of regex than english.

halo09876
  • 2,725
  • 12
  • 51
  • 71
  • The nested-for loop solutions are fine but they don't scale well. If you have huge amount of text and your text or query words are related by common prefixes, [this](https://stackoverflow.com/questions/43628742/find-lots-of-string-in-text-python/43629194) may help you. – Cong Ma Apr 28 '17 at 09:38

5 Answers5

8

List comprehension will work combined with the all function:

[all([k in s for k in words]) for s in strings]

This results in the following on your example:

[False, True, False]
languitar
  • 6,554
  • 2
  • 37
  • 62
2
strings = ['I have a bird', 'I have a bag and a bird', 'I have a bag']
words = ['bird','bag']


for string in strings:
  stringlist = string.split()
  word1 , word2 = words
  if word1 in stringlist and word2 in stringlist:
    print(True)
  else:
    print(False)

Result

False True False

Fuji Komalan
  • 1,979
  • 16
  • 25
  • I now get how things work, thanks! I didn't know I needed to split the strings before I try to find the words within the string. – halo09876 Apr 28 '17 at 09:31
1

This is it:

for substring in strings:
    k = [ w for w in words if w in substring ]
    print (len(k) == len(words) )
foxyblue
  • 2,859
  • 2
  • 21
  • 29
1

Use of function all() would be best option here, but point is doing without for loop. Here is solution using map/lambda function.

strings = ['I have a bird', 'I have a bag and a bird', 'I have a bag']
words = ['bird','bag']
map(lambda x: all(map(lambda y:y in x.split(),words)),strings)

output would be:

[False, True, False]

However, Naive Solution is for beginner:

for string in strings:
    count_match=0
    for word in words:
        if word in string.split():
            count_match+=1
    if(count_match==len(words)):
        print "True"
    else:
        print "False"

And output would be :

False
True
False
Aakash Goel
  • 982
  • 7
  • 12
1

For variable number of words, without using the split function.

strings = ['I have a bird', 'I have a bag and a bird', 'I have a bag']
words = ['bird','bag']

 for string in strings:
    print(all(word in string for word in words))
nikhalster
  • 481
  • 2
  • 9