📌  相关文章
📜  Python程序删除两个字符串中常见的单词

📅  最后修改于: 2021-09-05 11:41:45             🧑  作者: Mango

给定两个表示句子的字符串S1S2 ,任务是在删除两个句子中存在的所有单词后打印这两个句子。

使用哈希的方法可以使用Counter()函数解决问题。请按照以下步骤解决问题:

  • 由于句子中的所有单词都由空格分隔,因此使用split()将单词按空格拆分并将它们存储在List 中
  • 初始化两个列表,比如sentence1sentence2 ,以存储两个给定句子的单词。
  • 使用Counter()函数计算两个句子中单词的频率,并将其存储在字典frequency1frequency2 中
  • 遍历列表sentence1并删除出现在字典frequency2中的单词。
  • 遍历列表sentence2并删除出现在字典frequency1中的单词。
  • 打印这两个列表。

下面是上述方法的实现:

Python3
# Python program for the above approach
from collections import Counter
  
# Function to remove common
# words from two strings
def removeCommonWords(sent1, sent2):
    
    # Store the words present
    # in both the sentences
    sentence1 = list(sent1.split())
    sentence2 = list(sent2.split())
      
    # Calculate frequency of words
    # using Counter() function
    frequency1 = Counter(sentence1)
    frequency2 = Counter(sentence2)
  
  
    word = 0
      
    # Iterate the list consisting
    # of words in the first sentence 
    for i in range(len(sentence1)):
        
        # If word is present
        # in both the strings
        if sentence1[word] in frequency2.keys():
            
              # Remove the word
            sentence1.pop(word)
              
            # Decrease the frequency of the word
            word = word-1
        word += 1
          
    word = 0
      
    # Iterate the list consisting of
    # words in the second sentence 
    for i in range(len(sentence2)):
        
        # If word is present
        # in both the strings
        if sentence2[word] in frequency1.keys():
            
              # Remove the word
            sentence2.pop(word)
              
            # Decrease the removed word
            word = word-1
              
        word += 1
          
    # Print the remaining
    # words in the two sentences
    print(*sentence1)
    print(*sentence2)
  
  
# Driver Code
  
sentence1 = "sky is blue in color"
sentence2 = "raj likes sky blue color"
  
removeCommonWords(sentence1, sentence2)


Python3
# Python program to implement
# the above approach
  
# Function to return the words which
# are common in both the sentences
def commonWords(sent1, sent2):
    
    # Splitting the words in a set
    sen1 = set(sent1)
    sen2 = set(sent2)
      
    # Stores the list of common words
    common = list(sen1.intersection(sen2))
      
    # Return the list
    return common
  
# Function to remove all the words
# that are common in both the strings
def removeCommonWords(sent1, sent2):
    
    # Stores the words of the
    # sentences in separate lists
    sentence1 = list(sent1.split())
    sentence2 = list(sent2.split())
      
    # Find the words that are
    # common in both the sentences
    commonlist = commonWords(sentence1, 
                             sentence2)
  
    word = 0
      
    # Iterate the list of words
    # of the first sentence
    for i in range(len(sentence1)):
        
        # If word is common in both lists
        if sentence1[word] in commonlist:
            
              # Remove the word
            sentence1.pop(word)
              
            # Decrease the removed word
            word = word - 1
        word += 1
  
    word = 0
      
    # Iterate the list of words
    # of the second sentence
    for i in range(len(sentence2)):
        
        # If word is common in both lists
        if sentence2[word] in commonlist:
            
              # Remove the word
            sentence2.pop(word)
              
            # Decrease the removed word
            word = word-1
        word += 1
          
    # Print the remaining words
    # in both the sentences
    print(*sentence1)
    print(*sentence2)
  
  
# Driver Code
  
S1 = "sky is blue in color"
S2 = "Raj likes sky blue color"
  
removeCommonWords(S1, S2)


输出:
is in
raj likes


时间复杂度: O((max(N, M)) 2 )
辅助空间: O(max(N, M))

使用集合列表的方法按照以下步骤解决问题:

  • 由于句子中的所有单词都由空格分隔,因此使用split()将单词按空格拆分并将它们存储在List 中
  • 初始化两个列表,比如sentence1sentence2 ,以存储两个给定句子的单词。
  • 将两个 Lists 转换成 Sets,比如sen1sen2
  • 现在,找到两个集合集合交集要存储两个句子中常见的单词,请说common
  • 遍历列表SENTENCE1弹出所有中存在的两个句子的交集词语的
  • 对第二句话重复相同的操作。
  • 最后,打印两个Lists 中剩余的单词。

下面是上述方法的实现:

蟒蛇3

# Python program to implement
# the above approach
  
# Function to return the words which
# are common in both the sentences
def commonWords(sent1, sent2):
    
    # Splitting the words in a set
    sen1 = set(sent1)
    sen2 = set(sent2)
      
    # Stores the list of common words
    common = list(sen1.intersection(sen2))
      
    # Return the list
    return common
  
# Function to remove all the words
# that are common in both the strings
def removeCommonWords(sent1, sent2):
    
    # Stores the words of the
    # sentences in separate lists
    sentence1 = list(sent1.split())
    sentence2 = list(sent2.split())
      
    # Find the words that are
    # common in both the sentences
    commonlist = commonWords(sentence1, 
                             sentence2)
  
    word = 0
      
    # Iterate the list of words
    # of the first sentence
    for i in range(len(sentence1)):
        
        # If word is common in both lists
        if sentence1[word] in commonlist:
            
              # Remove the word
            sentence1.pop(word)
              
            # Decrease the removed word
            word = word - 1
        word += 1
  
    word = 0
      
    # Iterate the list of words
    # of the second sentence
    for i in range(len(sentence2)):
        
        # If word is common in both lists
        if sentence2[word] in commonlist:
            
              # Remove the word
            sentence2.pop(word)
              
            # Decrease the removed word
            word = word-1
        word += 1
          
    # Print the remaining words
    # in both the sentences
    print(*sentence1)
    print(*sentence2)
  
  
# Driver Code
  
S1 = "sky is blue in color"
S2 = "Raj likes sky blue color"
  
removeCommonWords(S1, S2)
输出:
is in
Raj likes


时间复杂度: O(max(N, M))
辅助空间: O(max(N, M))

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live