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

  • Home
  • SEARCH
  • 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 6056351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:22:00+00:00 2026-05-23T08:22:00+00:00

I need to generate the diff between two arrays of strings: a=[‘1′,’2’] b=[‘1′,’2′,’3’] To

  • 0

I need to generate the diff between two arrays of strings:

a=['1','2']
b=['1','2','3']

To achieve this I’m using the difflib library in Python (2.6):

c=difflib.unified_diff(a,b)

and I save the content of

d=list(c)

which is something like:

['---  \n', '+++  \n', '@@ -1,2 +1,3 @@\n', ' 1', ' 2', '+3']

How can I build the second array from the first using the output of the unified_diff function?

The behavior that I’m looking for is something like:

>>> merge(a,d)
>>> ['1','2','3']

P.S. the array can have duplicate entries and the order in which each entry appears is important for my application. Moreover, from one iteration to another there could be changes both in the middle/begin of the array, as well as new entries added at the end.

  • 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-05-23T08:22:01+00:00Added an answer on May 23, 2026 at 8:22 am

    Not sure that my sample is a good style, but you can use something like this:

    from collections import Counter
    
    a=['1','2']
    b=['1','2','3']
    
    a.extend(b)
    
    [k for k,v in Counter(a).items() if v == 1]
    

    OR if your lists could have only unique items:

    list(set(a) ^ set(b))
    

    OR:

    missed_in_a = [x for x in a if x not in b]
    missed_in_b = [x for x in b if x not in a]
    

    OR:

    a=['1','2']
    b=['1','2','3']
    
    c = [x for x in a]
    c.extend(b)
    
    diff = [x for x in c if a.count(x)+b.count(x) == 1]
    

    The last one(hope i understand you correctly(sorry if not so) now):

    a = ['1','2','3','4']
    b = ['2','2','3','6','5']
    
    from difflib import unified_diff
    
    def merge(a,b):
        output = []
        for line in list(unified_diff(a,b))[3:]:
            if '+' in line:
                output.append(line.strip('+'))
            elif not '-' in line:
                output.append(line.strip())
        return output
    
    print merge(a,b)        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate a random integer between 1 and n (where n is
I need to generate multiple random values under SQL Server 2005 and somehow this
I need to generate a .PO file from a .properties file using msgcat from
I'm writing a programm in which I need to generate a diff and display
I need the Numeric.FAD library, albeit still being completely puzzled by existential types. This
I've a number Between 10 and 30, I need to present this number in
I need to generate a random number, now this number needs to be somewhere
I am working on analyzing different files between two views in Clearcase. I need
If I need generate a fairly large dataset using LINQ and it may take
I need generate thumbnails for a bunch of jpegs (200,000+) but I want to

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.