Home > database >  How to use set.intersection in an if-statement?
How to use set.intersection in an if-statement?

Time:01-11

I want to check if multiple strings are within a larger string called "str_a". The following is what I currently have and it works.

animals = {"giraffe", "tiger"}
str_a = "A giraffe is taller than a tiger."

if "giraffe" in str_a or "tiger" in f:
    print ("T")
else:
    print ("F")

However, I wanted to represent the if-statement in a more concise manner and I think set.intersection can help me achieve that. I tried the following with set.intersection and it prints out "F" instead of "T" - I'm not sure why. Any guidance on this would be appreciated!

animals = {"giraffe", "tiger"}
str_a = "A giraffe is taller than a tiger."
matches = animals.intersection(str_a)

if matches:
    print ("T")
else:
    print ("F")

CodePudding user response:

To turn a string into a set of words, use .split():

>>> set(str_a.split())
{'giraffe', 'A', 'taller', 'tiger.', 'is', 'than', 'a'}

You can now do a set intersection:

>>> set(str_a.split())&animals
{'giraffe'}

Note that 'tiger.' is not the same as 'tiger'. To strip punctuation, you can use a set comprehension:

>>> {w.rstrip(',.:') for w in str_a.split()}
{'giraffe', 'tiger', 'A', 'taller', 'is', 'than', 'a'}

Then both will be found:

>>> {w.rstrip(',.:') for w in str_a.split()}&animals
{'giraffe', 'tiger'}

CodePudding user response:

You should instead use any:

s = set(str_a.split())
if any(animal in s for animal in animals):
    print('T')
else:
    print('F'

CodePudding user response:

You could use the isdisjoint method of your animals set. isdisjoint will tell you if all elements of animals are absent from the words in str_a (essentially the "F" result) without creating a second set.

animals = {"giraffe", "tiger"}
str_a = "A giraffe is taller than a tiger."

if not animals.isdisjoint(str_a.split()):
    print("T")
else:
    print("F")
  •  Tags:  
  • Related