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.

1
  • 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 may help you. Commented Apr 28, 2017 at 9:38

5 Answers 5

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]
Sign up to request clarification or add additional context in comments.

1 Comment

[all(word in sentence for word in words) for sentence in strings] :-)
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

1 Comment

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.
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

Comments

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))

Comments

1

This is it:

for substring in strings:
    k = [ w for w in words if w in substring ]
    print (len(k) == len(words) )

2 Comments

I would prefer this one but it only resulted in false false?
Sorry, I didn't understand your initial question - but i've fixed my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.