Open In App

Python | Common words among tuple strings

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with tuples, we can have a problem in which we need to find the intersection of words that occur in strings inside single tuple with string as its elements. Let’s discuss certain ways in which this problem can be solved.

 Method #1 : Using join() + set() + & operator + split() 

The combination of above functions can be used to perform this particular task. In this, we first convert each tuple to set and then perform the intersection of the element’s words splitted by split(). Last step is to join all common elements using join(). 

Python3




# Python3 code to demonstrate working of
# Common words among tuple strings
# Using join() + set() + & operator + split()
 
# Initializing tuple
test_tup = ('gfg is best', 'gfg is for geeks', 'gfg is for all')
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Common words among tuple strings
# Using join() + set() + & operator + split()
res = ", ".join(sorted(set(test_tup[0].split()) &
                       set(test_tup[1].split()) &
                       set(test_tup[2].split())))
 
# printing result
print("Common words among tuple are : " + res)


Output : 

The original tuple is : ('gfg is best', 'gfg is for geeks', 'gfg is for all')
Common words among tuple are : gfg, is

Method #2: Using map() + reduce() + lambda 

The combination of above methods can also be used to perform this particular task. In this, we just combine all elements of tuple to check for common elements using lambda and reduce(). The advantage of this method is that it can work with tuple with more than countable elements easily. Works with Python3 only. 

Python3




# Python3 code to demonstrate working of
# Common words among tuple strings
# Using map() + reduce() + lambda
 
# Initializing tuple
test_tup = ('gfg is best', 'gfg is for geeks', 'gfg is for all')
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Common words among tuple strings
# Using map() + reduce() + lambda
res = ", ".join(reduce(lambda i, j: i & j,
                       map(lambda x: set(x.split()), test_tup)))
 
# printing result
print("Common words among tuple are : " + res)


Output : 

The original tuple is : ('gfg is best', 'gfg is for geeks', 'gfg is for all')
Common words among tuple are : gfg, is

Method #3: Using set and intersection method with list comprehension

Approach: We first split each tuple string into a set of words, then use the set.intersection method to find the common words among all the sets, and finally return the common words as a set. We also join the common words into a string with a comma separator and print it.

Python3




def common_words_6(tuples):
   
    word_sets = [set(words.split()) for words in tuples]
    common = set.intersection(*word_sets)
    return common
   
# Example usage:
tuples = ('gfg is best', 'gfg is for geeks', 'gfg is for all')
 
# Printing result
print("Common words among tuple are :", ', '.join(common_words_6(tuples)))


Output

Common words among tuple are : is, gfg

Time complexity: O(nmlog(m)) where n is the number of tuples and m is the average length of each tuple string.
Auxiliary Space: O(m) – where m is the length of the longest tuple string.

Method 4 : Using a loop and set intersection

  1. Initialize an empty set named common_words to store common words.
  2. Split the first string of the tuple using the split() method and store the words in a set named words_set.
  3. Iterate through the remaining strings in the tuple using a loop.
  4. For each string, split the words using the split() method and find the intersection between the words_set and the new set using the & operator.
  5. Update the words_set to be the intersection set found in the previous step.
  6. Repeat steps 4-5 until all strings in the tuple have been iterated over.
  7. Join the words in the words_set using the join() method and store the result in the variable named res.
  8. Print the result.

Python3




# Python3 code to demonstrate working of
# Common words among tuple strings
# Using loop and set intersection
 
# Initializing tuple
test_tup = ('gfg is best', 'gfg is for geeks', 'gfg is for all')
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Common words among tuple strings
# Using loop and set intersection
common_words = set(test_tup[0].split())
for string in test_tup[1:]:
    common_words &= set(string.split())
 
res = ", ".join(sorted(common_words))
 
# printing result
print("Common words among tuple are : " + res)


Output

The original tuple is : ('gfg is best', 'gfg is for geeks', 'gfg is for all')
Common words among tuple are : gfg, is

Time complexity: O(N*M) where N is the number of strings in the tuple and M is the maximum length of a string in the tuple.
Auxiliary space: O(M) where M is the maximum length of a string in the tuple.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads