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 757967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:25:27+00:00 2026-05-14T15:25:27+00:00

I’d like to align two lists in a similar way to what difflib.Differ would

  • 0

I’d like to align two lists in a similar way to what difflib.Differ would do except I want to be able to define a match function for comparing items, not just use string equality, and preferably a match function that can return a number between 0.0 and 1.0, not just a boolean.

So, for example, say I had the two lists:

L1 = [('A', 1), ('B', 3), ('C', 7)]
L2 = ['A', 'b', 'C']

and I want to be able to write a match function like this:

def match(item1, item2):
    if item1[0] == item2:
        return 1.0
    elif item1[0].lower() == item2.lower():
        return 0.5
    else:
        return 0.0

and then do:

d = Differ(match_func=match)
d.compare(L1, L2)

and have it diff using the match function. Like difflib, I’d rather the algorithm gave more intuitive Ratcliff-Obershelp type results rather than a purely minimal Levenshtein distance.

  • 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-14T15:25:28+00:00Added an answer on May 14, 2026 at 3:25 pm

    I just wrote this implementation of Needleman-Wunsch and it seems to do what I want:

    def nw_align(a, b, replace_func, insert, delete):
    
        ZERO, LEFT, UP, DIAGONAL = 0, 1, 2, 3
    
        len_a = len(a)
        len_b = len(b)
    
        matrix = [[(0, ZERO) for x in range(len_b + 1)] for y in range(len_a + 1)]
    
        for i in range(len_a + 1):
            matrix[i][0] = (insert * i, UP)
    
        for j in range(len_b + 1):
            matrix[0][j] = (delete * j, LEFT)
    
        for i in range(1, len_a + 1):
            for j in range(1, len_b + 1):
                replace = replace_func(a[i - 1], b[j - 1])
                matrix[i][j] = max([
                    (matrix[i - 1][j - 1][0] + replace, DIAGONAL),
                    (matrix[i][j - 1][0] + insert, LEFT),
                    (matrix[i - 1][j][0] + delete, UP)
                ])
    
        i, j = len_a, len_b
        align_a = ""
        align_b = ""
    
        while (i, j) != (0, 0):
            if matrix[i][j][1] == DIAGONAL:
                align_a += a[i - 1]
                align_b += b[j - 1]
                i -= 1
                j -= 1
            elif matrix[i][j][1] == LEFT:
                align_a += "-"
                align_b += b[j - 1]
                j -= 1
            else: # UP
                align_a += a[i - 1]
                align_b += "-"
                i -= 1
    
        return align_a[::-1], align_b[::-1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 420k
  • Answers 420k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This transformation: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:iwwr="http://www.interwoven.com/schema/iwrr" > <xsl:output omit-xml-declaration="yes" indent="yes"/>… May 15, 2026 at 10:44 am
  • Editorial Team
    Editorial Team added an answer APC's answer is the way to go for a logical… May 15, 2026 at 10:44 am
  • Editorial Team
    Editorial Team added an answer Sockets are not allowed on Google App Engine: The restrictions… May 15, 2026 at 10:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.