[Tutor] finding common words using set.
mhysnm1964 at gmail.com
mhysnm1964 at gmail.com
Sun Jan 1 00:41:50 EST 2023
All,
I have created a function to find the common words within a list of strings.
I never get any results back of the common words and cannot understand why.
This is the first time I have delt with the set data methods and are trying
to understand how to use them.
def find_common_words(strings):
# Split the strings into lists of words
word_lists = [s.split() for s in strings]
# Find the intersection of the sets of words
common_words = set(word_lists[0]).intersection(*word_lists[1:])
# Join the common words into a single string
return ' '.join(common_words)
strings = ['apple orange banana', 'orange banana grape', 'banana mango']
common_words = find_common_words(strings)
print(common_words)
Should return banana orange and I get a null string. Any ideas why?
Sean
More information about the Tutor
mailing list