Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:32:31+00:00 2026-06-01T16:32:31+00:00

Given two strings, I’d like to be able to — in Python — be

  • 0

Given two strings, I’d like to be able to — in Python — be able to determine which words have been added and which words have been removed between the two. I’ve seen difflib, but apparently it cannot do it.

For example: given ‘hello my name is’ and ‘hello my guys is’, it would return [‘guys’] as added words, and [‘name’] as removed words. Thanks a lot.

EDIT: Probably the example I gave wasn’t the best. It should also work without spaces between the current text and the new text. Maybe using difflib to get all the new sections, and then split with regexp “\b”. I’ll give it a try.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:32:32+00:00Added an answer on June 1, 2026 at 4:32 pm

    This isn’t particularly pretty but seems to work for most cases I can think of. I’m sure this can be tidied up a lot too and should be easy to make case insensitive.

    def freqs(list):
        words = {}
        for word in list:
            words[word] = words.get(word, 0) + 1
        return words
    
    def added_and_removed(a, b):
        af = freqs(a.split())
        bf = freqs(b.split())
    
        removed = []
        added = []
    
        for key in af:
            num = bf.get(key)
            if num == None:
                if af[key] > 1:
                    words = [key]*af[key]
                    removed.extend(words)
                else:
                    removed.append(key)
    
        for key in bf:
            num = af.get(key)
            if num == None:
                added.append(key)
            elif num > 1:
                words = [key]*(num-1)
                removed.extend(words)
    
        return added, removed
    
    a = 'hello hello hello my name is Dave dave bar foo'
    b = 'hello my guys is test easy rob dave beef foo'     
    
    added, removed =  added_and_removed(a, b)
    print added
    print removed
    

    gives

    ['beef', 'rob', 'easy', 'test', 'guys']
    ['bar', 'name', 'Dave', 'hello', 'hello']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A problem I'm trying to solve: given that you have two distinct strings composed
I have a string say 123dance456 which I need to split into two strings
Given are two python lists with strings in them (names of persons): list_1 =
Given two strings, how can I do something if both strings have one word
I have two strings. How can I determine whether the first string is composed
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Given the following strings 7;#User One 7;#User Two;#9;#User Two 7;#User Two;#9;#User Two;#123;#User Three I
Given: two images of the same subject matter; the images have the same resolution,
Given two planes positioned and rotated arbitrary to each other, I'm trying to determine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.